The 2016 Google I/O announced their new product – Firebase – a cloud platform with lots of spectacular features for the mobile app developers. And one of these features includes Firebase notifications.
This is a tutorial about sending push notifications to Android through Firebase, based on the new release of Firebase this year (2016). This tutorial shows how to setup the skeleton for sending and receiving push notifications via FCM with instructions on server code.
The 2016 Google I/O announced their new product – Firebase – a cloud platform with lots of spectacular features for the mobile app developers. And one of these features includes Firebase notifications.
This is a tutorial about sending push notifications to Android through Firebase, based on the new release of Firebase this year (2016). This tutorial shows how to setup the skeleton for sending and receiving push notifications via FCM with instructions on server code.
Before We Start
You’ll need the following:
- The latest stable Android Studio
- AFirebase account.
- Latest Cordova installed
Get started
First create a sample Cordova App
$ Cordova create pushDemo
$ Cd pushDemo
$ Cordova platform add android
$ cordova plugin add cordova-plugin-firebase@0.1.19 –save
Create new project or import an existing project to Firebase console
If you choose to create a new project, you need to set your project name and your country name.
After that select “Add Firebase to your Android app”.
Write package name of your app. You can only set your package name and omit the SHA-1 if you don’t use Firebase for app’s authentication.
Click add app button to download google-services.json. This is most important file and you will need to put this file in your app folder.
getToken
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);
});
onTokenRefresh
Register for token changes:
window.FirebasePlugin.onTokenRefresh(function(token) {
// save this serverside and use it to push notifications to this device
console.log(token);},
function(error) { console.error(error);});
Run App.
Run your app to test push notification.
$ cordova run android
Test and send your first push notification!
To see if the setup works, run a test by sending a test message to your own mobile.
Write down your message and choose an app. Click “SEND MESSAGE”.
Now you should get a push notification on your Android mobile.