Official link https://firebase.google.com/docs/cloud-messaging
Get the device token (id):
window.FirebasePlugin.getToken(function(token) {
// save this server-side and use it to push notifications to this device
console.log(token);
}, function(error) {
console.error(error);
});
Note that token will be null if it has not been established yet
Register for token changes:
window.FirebasePlugin.onTokenRefresh(function(token) {
// save this server-side and use it to push notifications to this device
console.log(token);
}, function(error) {
console.error(error);
});
This is the best way to get a valid token for the device as soon as the token is established
Register notification callback:
window.FirebasePlugin.onNotificationOpen(function(notification) {
console.log(notification);
}, function(error) {
console.error(error);
});
Notification flow:
- App is in foreground:
- User receives the notification data in the JavaScript callback without any notification on the device itself (this is the normal behaviour of push notifications, it is up to you, the developer, to notify the user)
- App is in background:
- User receives the notification message in its device notification bar
- User taps the notification and the app opens
- User receives the notification data in the JavaScript callback
Notification icon on Android:
Grant permission to receive push notifications (will trigger prompt):
window.FirebasePlugin.grantPermission();
Check permission to receive push notifications:
window.FirebasePlugin.hasPermission(function(data){
console.log(data.isEnabled);
});
Set a number on the icon badge:
window.FirebasePlugin.setBadgeNumber(3);
Set 0 to clear the badge
window.FirebasePlugin.setBadgeNumber(0);
Get icon badge number:
window.FirebasePlugin.getBadgeNumber(function(n) {
console.log(n);
});
Clear all pending notifications from the drawer:
window.FirebasePlugin.clearAllNotifications();
Subscribe to a topic:
window.FirebasePlugin.subscribe("example");
Unsubscribe from a topic:
window.FirebasePlugin.unsubscribe("example");
Unregister from firebase, used to stop receiving push notifications. Call this when you logout user from your app. :
window.FirebasePlugin.unregister();