如何使用推送通知进行可信的Web活动

时间:2019-03-29 23:40:05

标签: android push-notification web-push trusted-web-activity

在本教程中,我已经使用TWA为网站成功创建了一个APK。

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

但是我不知道如何为我的apk添加推送通知。有两种方法:1. Web推送2-android push。他们两个都有单独的SDK。

问题是,如果我使用web-push,Chrome如何知道它不应该访问网站,而应该访问应用程序。

而且我也有使用android sdk进行推送通知的问题。推送教程说您应该在主要活动的onCreate事件中放入一些代码。而且我的项目(由twa教程制作)没有任何活动。

3 个答案:

答案 0 :(得分:0)

教程中的步骤之一说明了如何设置App Links,以便在其中打开指向在“受信任的Web活动”中打开的URL的域的链接-这也适用于Web推送链接。这是本教程的相关部分:

FROM mysql:5.7 as builder # needed for intialization ENV MYSQL_ROOT_PASSWORD=somepassword ADD initialize.aql /docker-entrypoint-initdb.d/ # That file does the DB initialization but also runs mysql daemon, by removing the last line it will only init RUN ["sed", "-i", "s/exec \"$@\"/echo \"not running $@\"/", "/usr/local/bin/docker-entrypoint.sh"] RUN ["/usr/local/bin/docker-entrypoint.sh", "mysqld", "--datadir", "/initialized-db"] FROM mysql:5.7 COPY --from=builder /initialized-db /var/lib/mysql 标记内:

activity

用您在TWA中打开的域替换 <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>

关于第二个问题,该演示利用了一个活动Activity,它是支持库LauncherActivity的一部分。为了编写自己的airhorner.com,您将需要拥有自己的Activity。一种方法是将“活动”中的代码从支持库复制到您自己的代码中,然后根据需要更改onCreate

答案 1 :(得分:0)

如果您使用的是Firebase云消息传递,则在TWA中,您可以使用网络推送,在您的网站上接收它们,或android本机推送,在您的应用程序中接收它们。

我的实验表明,网络推送非常不可靠。它们实际上是由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://stackoverflow.com/a/58069713/8818281

答案 2 :(得分:0)

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

了解如何向查询参数添加值会很有帮助,检查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

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

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

注意:我发现如果我们只使用通知委托,通知会在通知中显示您的网站 URL 而不是应用名称,但如果我们使用共享 fcm_token 方法,那么您可以从原生 android 代码更改应用名称。