Cordova push plugin with firebase is used to send notifications in both android and iOS devices. This plugin can be used for Cordova applications with Google Firebase FCM. You can send notifications to one or multiple devices with this plugin.
So, in this tutorial I will guide you to "Use Cordova Push Plugin with Google Firebase Cloud Messaging".
Let's get started:
Installation:
To install this plugin, run the following command into your cordova project:
cordova plugin add cordova-plugin-fcm
Requirements:
- Tested on Android and iOS using Cordova cli 6.4.0, Cordova android 6.0.0 and Cordova ios 4.3.1
- 'google-services.json' and 'GoogleService-Info.plist' are required to add into your project. you can download the configuration files for Android or iOS from the Firebase Console ( https://firebase.google.com/docs/). Put the downloaded file in the Cordova project root folder.
Receiving Token Refresh:
//FCMPlugin.onTokenRefresh( onTokenRefreshCallback(token) );
//Note that this callback will be fired everytime a new token is generated, including the first time.
FCMPlugin.onTokenRefresh(function(token){
alert( token );
});
Get token:
//FCMPlugin.getToken( successCallback(token), errorCallback(err) );
//Keep in mind the function will return null if the token has not been established yet.
FCMPlugin.getToken(function(token){
alert(token);
});
Subscribe to topic:
//FCMPlugin.subscribeToTopic( topic, successCallback(msg), errorCallback(err) );
//All devices are subscribed automatically to 'all' and 'ios' or 'android' topic respectively.
//Must match the following regular expression: "[a-zA-Z0-9-_.~%]{1,900}".
FCMPlugin.subscribeToTopic('topicExample');
Unsubscribe from topic:
//FCMPlugin.unsubscribeFromTopic( topic, successCallback(msg), errorCallback(err) );
FCMPlugin.unsubscribeFromTopic('topicExample');
Receiving push notification data:
//FCMPlugin.onNotification( onNotificationCallback(data), successCallback(msg), errorCallback(err) )
//Here you define your application behaviour based on the notification data.
FCMPlugin.onNotification(function(data){
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert( JSON.stringify(data) );
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert( JSON.stringify(data) );
}
});
0 Comment(s)