简体   繁体   English

如何对受信任的 Web 活动使用推送通知

[英]How to use push notification for trusted web activity

I have successfully created an apk for my webesite using TWA with this tutorial.我已经使用 TWA 和本教程成功地为我的网站创建了一个 apk。

https://developers.google.com/web/updates/2019/02/using-twa https://developers.google.com/web/updates/2019/02/using-twa

But I don't know How should I add push notification for my apk.但我不知道我应该如何为我的 apk 添加推送通知。 there are two methods: 1. Web-push 2-android push.有两种方法: 1. Web-push 2-android push。 which both of them have separate SDKs.他们都有单独的SDK。

The question is if I use web-push how does chrome know that it should not go to the website and it should go to app.问题是,如果我使用 web-push,chrome 怎么知道它不应该去网站,而应该去 app.

And Also I have problem using android sdk for push notification too.而且我也有使用 android sdk 进行推送通知的问题。 The tutorial for push says you should put some code in onCreate event of main activity. push 的教程说你应该在主要活动的 onCreate 事件中放置一些代码。 And my project (made with twa tutorial) has no activity.我的项目(用 twa 教程制作)没有活动。

One of the steps in the tutorial explains how to setup App Links so that links to the domain of the URL being opened in the Trusted Web Activity are opened inside it - This also works for the web push links.本教程中的步骤之一解释了如何设置应用链接,以便在受信任的 Web 活动中打开的 URL 域的链接在其中打开 - 这也适用于网络推送链接。 Here's the relevant section of the tutorial:这是本教程的相关部分:

Inside the activity tag:activity标签内:

 <intent-filter>
   <action android:name="android.intent.action.VIEW"/>
     <category android:name="android.intent.category.DEFAULT" />
     <category android:name="android.intent.category.BROWSABLE"/>

     <!-- Edit android:host to handle links to the target URL-->
     <data
       android:scheme="https"
       android:host="airhorner.com"/>
 </intent-filter>

Replace airhorner.com by the domain you are opening inside the TWA.airhorner.com替换为您在 TWA 内打开的域。

Regarding the second question, the demo makes use of an utility Activity that is part of the Support Library, LauncherActivity .关于第二个问题,演示使用了一个实用程序 Activity,它是支持库LauncherActivity 的一部分 In order to write your own onCreate , you will need to have your own Activity.为了编写自己的onCreate ,您需要拥有自己的 Activity。 One approach is copy the code from the Activity from the Support Library into your own code, and change onCreate as needed.一种方法是将来自支持库的 Activity 的代码复制到您自己的代码中,并根据需要更改onCreate

If you are using firebase cloud messaging, in TWA you can use web pushes, receiving them on your site, or android native pushes, receiving them in your application.如果您使用 firebase 云消息传递,则在 TWA 中,您可以使用网络推送,在您的网站上接收它们,或使用 Android 原生推送,在您的应用程序中接收它们。

My experiments have shown web pushes very unreliable.我的实验表明网络推送非常不可靠。 They are actually received by chrome and depends on chrome settings and policies.它们实际上由 chrome 接收并取决于 chrome 设置和策略。 Very likely they won't be shown as notification popup with sound, only as the notification icon.它们很可能不会显示为带有声音的通知弹出窗口,只会显示为通知图标。

Alternatively you can write a bit more complex application, which uses firebase android sdk and receives native pushes.或者,您可以编写一个更复杂的应用程序,它使用 firebase android sdk 并接收本机推送。 Native pushes are completelly reliable, you can control their importance and look as you wish.本机推送完全可靠,您可以控制它们的重要性并随心所欲。

You will have to create the main activity manually, and place there any startup code you need:您必须手动创建主要活动,并在其中放置您需要的任何启动代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // override the default channel settings
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Create channel to show notifications.
        String channelId  = getString(R.string.default_notification_channel_id);
        String channelName = getString(R.string.default_notification_channel_name);
        NotificationManager notificationManager =
                getSystemService(NotificationManager.class);
        // override the default channel importance to make notifications show as popup with sound
        notificationManager.createNotificationChannel(new NotificationChannel(channelId,
                channelName, NotificationManager.IMPORTANCE_HIGH));
    }

    //
    // here you can get the device registration token and send it to your backend
    // or do any additional processing
    //

    // now everithing is set up and we can start the twa activity
    Intent intent = new Intent(this, com.google.androidbrowserhelper.trusted.LauncherActivity.class);
    intent.setData(Uri.parse("http://www.google.com"));
    startActivity(intent);
}

Some more details on starting TWA activity programmatically in this post: https://stackoverflow.com/a/58069713/8818281在这篇文章中以编程方式启动 TWA 活动的更多详细信息: https : //stackoverflow.com/a/58069713/8818281

Update : I managed to get the native android notification using query params, basically the idea is that you can share data from android to TWA activity using query params, So you can add fcm-token to the query param before opening TWA activity and read the fcm-token in your web-application.更新:我设法使用查询参数获得了原生 android 通知,基本上这个想法是您可以使用查询参数将数据从 android 共享到 TWA 活动,因此您可以在打开 TWA 活动之前将 fcm-token 添加到查询参数并阅读网络应用程序中的 fcm-token。 then you can share it simply with your server using web-application logic.然后您可以使用 Web 应用程序逻辑简单地与您的服务器共享它。

It will be helpful to know how to add values to query params, Check https://github.com/GoogleChrome/android-browser-helper/blob/main/demos/twa-firebase-analytics/src/main/java/com/google/androidbrowserhelper/demos/twa_firebase_analytics/FirebaseAnalyticsLauncherActivity.java了解如何为查询参数添加值会很有帮助,检查https://github.com/GoogleChrome/android-browser-helper/blob/main/demos/twa-firebase-analytics/src/main/java/com /google/androidbrowserhelper/demos/twa_firebase_analytics/FirebaseAnalyticsLauncherActivity.java

Previous : Using Web push you can now override the notification prompt for Trusted Web Activity using native code and take advantage of native Android notification features.上一篇: 使用 Web 推送,您现在可以使用本机代码覆盖受信任的 Web 活动的通知提示,并利用本机 Android 通知功能。

Refer https://github.com/GoogleChrome/android-browser-helper/tree/main/demos/twa-notification-delegation参考https://github.com/GoogleChrome/android-browser-helper/tree/main/demos/twa-notification-delegation

Note: I found that if we use only notification delegation, the notification will show your website URL instead of App name in notification, but if we use sharing fcm_token method then you can change App name from native android code.注意:我发现如果我们只使用通知委托,通知会在通知中显示您的网站 URL 而不是应用名称,但如果我们使用共享 fcm_token 方法,那么您可以从原生 android 代码更改应用名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM