5

我已经使用 TWA 和本教程成功地为我的网站创建了一个 apk。

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

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

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

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

4

3 回答 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根据需要进行更改。

于 2019-04-01T07:10:53.290 回答
1

如果您使用 Firebase 云消息传递,在 TWA 中,您可以使用 Web 推送,在您的网站上接收它们,或者使用 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

于 2019-09-23T21:18:06.090 回答
0

更新:我设法使用查询参数获取本机 android 通知,基本上这个想法是您可以使用查询参数将数据从 android 共享到 TWA 活动,因此您可以在打开 TWA 活动之前将 fcm-token 添加到查询参数并阅读您的网络应用程序中的 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

Previous : 使用 Web 推送,您现在可以使用原生代码覆盖 Trusted Web Activity 的通知提示,并利用原生 Android 通知功能。

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

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

于 2021-03-03T13:06:30.120 回答