在本教程中,我已经成功地使用TWA为我的网站创建了一个apk。
https://developers.google.com/web/updates/2019/02/using-twa
但我不知道该如何为我的apk添加推送通知。有两种方式: 1.网页推送2-android推送。它们都有独立的SDK。
问题是,如果我使用web-push,chrome如何知道它不应该访问网站,而应该访问应用程序。
而且我在使用android sdk进行推送通知时也遇到了问题。push的教程说你应该在main activity的onCreate事件中放一些代码。我的项目(用twa tutorial制作)没有任何活动。
发布于 2019-04-01 15:10:53
本教程中的一个步骤解释了如何设置App Links,以便在可信网络活动中打开的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中打开的域。
关于第二个问题,演示程序使用了一个实用程序活动,该活动是支持库LauncherActivity的一部分。为了编写自己的onCreate,您需要有自己的Activity。一种方法是将活动中的代码从Support Library复制到您自己的代码中,并根据需要更改onCreate。
发布于 2019-09-24 05:18:06
如果您使用的是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://stackoverflow.com/a/58069713/8818281
发布于 2021-03-03 21:06:30
更新:我设法使用查询参数获得了原生的android通知,基本上的想法是你可以使用查询参数将数据从android共享到TWA活动,这样你就可以在打开TWA活动之前将fcm-token添加到查询参数中,并在你的web应用程序中读取fcm-token。然后,您可以使用web应用程序逻辑简单地与您的服务器共享它。
Android之前的:使用网页推送,你现在可以使用本地代码覆盖可信网络活动的通知提示,并利用本地Android通知功能。
参考https://github.com/GoogleChrome/android-browser-helper/tree/main/demos/twa-notification-delegation
注意:我发现如果我们只使用通知委派,通知将在通知中显示您的网站地址而不是应用程序名称,但如果我们使用共享fcm_token方法,那么您可以从本地安卓代码更改应用程序名称。
https://stackoverflow.com/questions/55426730
复制相似问题