简体   繁体   中英

How to use push notification for trusted web activity

I have successfully created an apk for my webesite using TWA with this tutorial.

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

But I don't know How should I add push notification for my apk. there are two methods: 1. Web-push 2-android push. which both of them have separate SDKs.

The question is if I use web-push how does chrome know that it should not go to the website and it should go to app.

And Also I have problem using android sdk for push notification too. The tutorial for push says you should put some code in onCreate event of main activity. And my project (made with twa tutorial) has no activity.

One of the steps in the tutorial explains how to setup App Links so that links to the domain of the URL being opened in the Trusted Web Activity are opened inside it - This also works for the web push links. Here's the relevant section of the tutorial:

Inside the activity tag:

 <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>

Replace airhorner.com by the domain you are opening inside the TWA.

Regarding the second question, the demo makes use of an utility Activity that is part of the Support Library, LauncherActivity . In order to write your own onCreate , you will need to have your own Activity. One approach is copy the code from the Activity from the Support Library into your own code, and change onCreate as needed.

If you are using firebase cloud messaging, in TWA you can use web pushes, receiving them on your site, or android native pushes, receiving them in your application.

My experiments have shown web pushes very unreliable. They are actually received by chrome and depends on chrome settings and policies. Very likely they won't be shown as notification popup with sound, only as the notification icon.

Alternatively you can write a bit more complex application, which uses firebase android sdk and receives native pushes. Native pushes are completelly reliable, you can control their importance and look as you wish.

You will have to create the main activity manually, and place there any startup code you need:

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);
}

Some more details on starting TWA activity programmatically in this post: https://stackoverflow.com/a/58069713/8818281

Update : I managed to get the native android notification using query params, basically the idea is that you can share data from android to TWA activity using query params, So you can add fcm-token to the query param before opening TWA activity and read the fcm-token in your web-application. then you can share it simply with your server using web-application logic.

It will be helpful to know how to add values to query params, Check 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 : Using Web push you can now override the notification prompt for Trusted Web Activity using native code and take advantage of native Android notification features.

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

Note: I found that if we use only notification delegation, the notification will show your website URL instead of App name in notification, but if we use sharing fcm_token method then you can change App name from native android code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM