Google Firebase Cloud Messaging Cordova Push Plugin
Extremely easy plug&play push notification plugin for Cordova applications with Google Firebase FCM.
Version 5.0.1 (09/19/2019)
- Fix google-services (downgrade version) to make it compatible with cordova-android 8.1.0.
Version 5.0.0 (09/17/2019)
- Updates google-services to make it compatible with cordova-android 8.1.0.
Version 4.1.3 (09/02/2019)
- Fix Analytic issues on Android devices.
Version 4.1.2 (08/19/2019)
- Adds Google Analytics Events Tracking support (Android API level 27+).
- Fix builder on development mode for Android.
Version 4.0.0 (06/28/2019)
- Adds support for Google Analytics Events Tracking (
logEvent
, setUserId
and setUserProperty
) (only iOS). - This wrapper
@ionic-native/fcm
is not required any more. You need to install a new one FCMNG
Version 3.0.4 (06/23/2019)
- Upgrade Android plugin for Gradle, revision 1.1.3 (March 2015)
Version 3.0.2 (05/09/2019)
Version 2.1.2 (03/06/2017)
- Tested on Android and iOS using Cordova cli 6.4.0, Cordova android 6.0.0 and Cordova ios 4.3.1
- Available sdk functions: onTokenRefresh, getToken, subscribeToTopic, unsubscribeFromTopic and onNotification
- 'google-services.json' and 'GoogleService-Info.plist' are added automatically from Cordova project root to platform folders
- Added data payload parameter to check whether the user tapped on the notification or was received while in foreground.
Installation
Make sure you have ‘google-services.json’ for Android or ‘GoogleService-Info.plist’ for iOS in your Cordova project root folder. You don´t need to configure anything else in order to have push notification working for both platforms, everything is magic.
cordova plugin add cordova-plugin-fcm-ng
Firebase configuration files
Get the needed configuration files for Android or iOS from the Firebase Console (see docs: https://firebase.google.com/docs/).
Android compilation details
Put the downloaded file 'google-services.json' in the Cordova project root folder.
You will need to ensure that you have installed the appropiate Android SDK libraries.
For Android >5.0 status bar icon, you must include transparent solid color icon with name fcm_push_icon.png
in the 'res' folder in the same way you add the other application icons.
If you do not set this resource, then the SDK will use the default icon for your app which may not meet the standards for Android >5.0.
iOS compilation details
Put the downloaded file 'GoogleService-Info.plist' in the Cordova project root folder.
Usage
It's highly recommended to use REST API to send push notifications because Firebase console does not have all the functionalities. Pay attention to the payload example in order to use the plugin properly.
Receiving Token Refresh
FCMPluginNG.onTokenRefresh(function(token){
alert( token );
});
Get token
FCMPluginNG.getToken(function(token){
alert(token);
});
Subscribe to topic
FCMPluginNG.subscribeToTopic('topicExample');
Unsubscribe from topic
FCMPluginNG.unsubscribeFromTopic('topicExample');
Receiving push notification data
FCMPluginNG.onNotification(function(data){
if(data.wasTapped){
alert( JSON.stringify(data) );
}else{
alert( JSON.stringify(data) );
}
});
Send notification. Payload example (REST API)
Full documentation: https://firebase.google.com/docs/cloud-messaging/http-server-ref
{
"notification":{
"title":"Notification title",
"body":"Notification body",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
},
"data":{
"param1":"value1",
"param2":"value2"
},
"to":"/topics/topicExample",
"priority":"high",
"restricted_package_name":""
}
How it works
Send a push notification to a single device or topic.
- 1.a Application is in foreground:
- The notification data is received in the JavaScript callback without notification bar message (this is the normal behaviour of mobile push notifications).
- 1.b Application is in background or closed:
- The device displays the notification message in the device notification bar.
- If the user taps the notification, the application comes to foreground and the notification data is received in the JavaScript callback.
- If the user does not tap the notification but opens the applicacion, nothing happens until the notification is tapped.
Usage of Google Analytics Methods
Every method returns a promise that fulfills when a call was successful.
logEvent(name, params)
Logs an app event.
FCMPluginNG.analytics.logEvent("my_event", {param1: "value1"});
Be aware of automatically collected events.
setUserId(id)
Sets the user ID property.
FCMPluginNG.setUserId("12345");
This feature must be used in accordance with Google's Privacy Policy.
setUserProperty(name, value)
Sets a user property to a given value.
FCMPluginNG.setUserProperty("name1", "value1");
License
The MIT License
Copyright (c) 2017 Felipe Echanique Torres (felipe.echanique in the gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.