In this blog, I am going to integrate the push notification in my Titanium app using the FCM module provide by the
Hans ( hansemannn) . I have used another module for FCM but that was not working on devices running Oreo and i was looking for other modules and i came across this module and it was working fine on Oreo devices and it also provides me option to customise the notifications also.
Integrating FCM
Assuming that you are familiar with Firebase cloud messaging, I am going to create a new project in the Firebase console. you can go to the Firebase console by using this
URL.
you have to click on Add project to create a new Firebase project then it will open a pop up box where you can enter the details of your new app.
I have entered app name as NotificationDemo1 and you can also add Firebase to your existing app by selecting it from the dropdown menu and you have two select the two checkboxes and clicl on create project.
We are doing it for Android so we have to click on Android icon and then we will get the option to register our app on the Firebase where we have to provide information of package name of our Android app, App nickname ( optional ) and Debug Signing Certificate SHA-1 ( optional) and you have to register you application.
You have to click on register app and then you have to download the google-services.json file which contains
project-id's and tracking-id's and such, generated by the Google API developer console into the google-services.json file. You have to click the next button for the the rest of process and when you came to step 4 then you have to "Run your app to verify installation" click the skip this steps button and Now, we have successfully completed the firebase project.
Titanium Integration
We will add the firebase.cloudmessaging, firebase.core and ti.playservices modules to our tiapp.xml file. you can read more about this module over
here. We also have to add the google-services.json file to our assets/android folder which will picked up by the module for information related to the firebase app. You will get your token inside the console window.
Index.js
// Configure core module (required for all Firebase modules).
var core = require('firebase.core');
core.configure({
file : "google-services.json"
});
var fcm = require('firebase.cloudmessaging');
function registerAndroid() {
// Called when the Firebase token is registered or refreshed.
fcm.addEventListener('didRefreshRegistrationToken', function(e) {
Ti.API.info('Token', e.fcmToken);
Ti.App.Properties.setString('push_token', e.fcmToken);
});
// Called when direct messages arrive. Note that these are different from push notifications.
fcm.addEventListener('didReceiveMessage', function(e) {
Ti.API.info('Message', e.message);
});
// Register the device with the FCM service.
fcm.registerForPushNotifications();
// Check if token is already available.
if (fcm.fcmToken) {
Ti.API.info('FCM-Token', fcm.fcmToken);
Ti.App.Properties.setString('push_token', fcm.fcmToken);
} else {
Ti.API.info('Token is empty. Waiting for the token callback ...');
}
}
registerAndroid();
$.index.open();
tiapp.xml
<modules>
<module platform="android">firebase.cloudmessaging</module>
<module platform="android">firebase.core</module>
<module platform="android">ti.playservices</module>
</modules>
Postman
I have used postman to send data message . You have to copy Legacy Server Key from Firebase console > Project Settings > Cloud Messaging
You have to add below header to your Post request.
Authorization : Key=<Legacy_server_key> or Key=<server_key>
Content-Type: application/json
You have to copy the device token from the console window and you have to paste it inside the registration_ids array inside the body of post request. If you wanted to send notification to more then one devices then token should be comma separated inside the registration_ids field.
you can import the postman collection from this
link.
This is how it will look in the device when you trigger the notification from postman.
Cheers !!!
Comments
Post a Comment