-
Notifications
You must be signed in to change notification settings - Fork 122
PWA : Push Notifications
Implementing Push Notification on GMD is very easy, we provided a Manager Class to easily control your Push Notification feature called PushNotificationManager which provided the subscription of the Client's app to your Web-Push Server later on we will discuss how we can build our Web-Push Server. We marry the PushNotification directly to the Service Worker to listen any push messages thrown by our Server. This will allow the browser also to receive push notifications even it was already closed (That's the benefit of wiring the Service Worker + Push Notification).
Some links for Basic Introduction of Service Worker can be found here.
@Override
public void onRegistered(ServiceWorkerRegistration registration) {
pushNotificationManager = new PushNotificationManager(registration);
pushNotificationManager.load(subscription -> {
if (subscription == null) {
MaterialToast.fireToast("Not subscribed Push Notifications");
} else {
MaterialToast.fireToast("Subscribed to Push Notifications");
}
});
}When your ServiceWorkerManager#onRegistered lifecycle setup your PushNotificationManager that accepts your ServiceWorkerRegistration. Load the PushNotificationManager and will detect if the user subscription was already subscribed or not.
// This public method will be used later to subscribe the user. Take note we provided a public key here to be used later on to our server web-pushed.
public void subscribe(Functions.Func callback) {
pushNotificationManager.subscribe(true, "BAvr2GL1EQdLnxgQDVeZSXnsWYNSaBbIkq4DsWQXwnpGrqXoGp_7YK0CiSPvszzPnAj-D49Ne-zKDBRWHHXBL1c", subscription -> {
if (subscription != null) {
sendSubscriptionToServer(subscription);
callback.call();
}
});
}