如果您使用 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