如何为可信网络活动使用推送通知

6
我已成功使用此教程使用TWA为我的网站创建apk文件。

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

但我不知道应该如何为我的APK添加推送通知,有两种方法:1. Web-push 2-Android push都有单独的SDK。
问题是如果我使用Web-push,Chrome怎么知道它不应该去网站,而应该进入app。
并且我也遇到使用Android SDK发送推送通知的问题。 推送通知的教程说你应将一些代码放在主Activity的onCreate事件中。 而我的项目(使用TWA教程制作)没有Activity。
3个回答

2

教程中的其中一步解释了如何设置应用链接,以便在受信任的Web活动中打开对URL域的链接时,在其中打开 - 这也适用于Web推送链接。以下是教程的相关部分:

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>

airhorner.com替换为您在TWA中打开的域名。

关于第二个问题,演示使用了支持库中的实用程序Activity LauncherActivity。为了编写自己的onCreate,您需要拥有自己的Activity。一种方法是将支持库中Activity的代码复制到自己的代码中,并根据需要更改onCreate

最初的回答:

要在TWA中打开不同的域名,请将airhorner.com替换为您想要打开的域名。

关于第二个问题,演示使用了一个叫做LauncherActivity的工具Activity,它是Support Library的一部分。如果您想要编写自己的onCreate,则需要拥有自己的Activity。一种方法是将Support Library中的Activity代码复制到您的代码中,并根据需要进行修改。


2
如果您正在使用 Firebase 云消息传递,在 TWA 中您可以使用 Web 推送,在您的站点上接收它们,或者使用 Android 原生推送,在应用程序中接收它们。
我的实验表明,Web 推送非常不可靠。它们实际上是由 Chrome 接收的,并且取决于 Chrome 的设置和策略。很可能它们不会显示为带有声音的通知弹出窗口,只会作为通知图标显示。
或者您可以编写一个稍微复杂一些的应用程序,该应用程序使用 Firebase Android SDK 并接收原生推送。原生推送是完全可靠的,您可以控制它们的重要性和外观。
您将需要手动创建主活动,并在其中放置任何所需的启动代码:
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);
}

以下是编程启动TWA活动的更多详细信息:https://dev59.com/rbbna4cB1Zd3GeqPUg3S#58069713


0

更新:我成功地使用查询参数获取了本机Android通知,基本上的想法是您可以使用查询参数从Android共享数据到TWA活动,因此您可以在打开TWA活动之前将fcm-token添加到查询参数中,并在Web应用程序中读取fcm-token。然后,您可以使用Web应用程序逻辑将其简单地与服务器共享。

了解如何向查询参数添加值将非常有帮助,请查看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

以前: 使用Web推送,您现在可以使用本机代码覆盖可信Web活动的通知提示,并利用本机Android通知功能。

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

注意:我发现如果我们仅使用通知委托,通知将显示您的网站URL而不是应用程序名称,但如果我们使用共享fcm_token方法,则可以从本机Android代码更改应用程序名称。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接