Thursday, June 20, 2019

Kony Push Notification using FCM

Steps

1) Create new project in kony visualizer 8.x version.

2) Go to firebase console, create a new project and select android. Then provide the package name taken from kony visualizer->settings->native->android.

3) From firebase messaging part, note down the server key and sender id.

4) In kony visualizer, create functions for kony.push.register api with the sender id from firebase. and kony.push.setCallback api for all callback functions.

5) Go to kony mobile fabric -> Engagement. Click android and in the field for server key, provide the server key taken from firebase and save.

6) Publish application in mobile fabric.

7) Associate the fabric application to visualizer project.

8) Select FCM in push notification under Kony visualizer->settings->native->android.

9) Take build for android.

10) Once you call the kony.push.register api with the sender id, Your device will be registered in kony engagement server. So that from Adhoc, you will be able to send push notification to selected devices.

Program

var KMSPROP = {};
var pushCallbacks = {
onsuccessfulregistration: regSuccessCallback,
onfailureregistration: regFailureCallback,
onlinenotification: onlinePushNotificationCallback,
offlinenotification: offlinePushNotificationCallback,
onsuccessfulderegistration: unregSuccessCallback,
onfailurederegistration: unregFailureCallback
};

/*
 * @function setAllCallbacks
 * this function will set the call backs for push notification 
 */
function setAllCallbacks() {
try {
kony.push.setCallbacks(pushCallbacks);
} catch (err) {
kony.print("KMS Module" + JSON.stringify(err));
}
}
/*
 * @function registerKMS
 * this function will register the divice for GMS console and APS console using
 * register API
 */
function registerKMS() {
try {
//#ifdef android
KMSPROP.osType = "androidgcm"; // ostype to sent if it is android
var configRegister = {
senderid: "sender id from firebase"
};
//#endif
//#ifdef iphone
KMSPROP.osType = "iphone";
var configRegister = [0, 1, 2];
//#endif
//this will check whether the device is al
if (kony.store.getItem("isRegisteredForKMS") === undefined || kony.store.getItem("isRegisteredForKMS") === "" || kony.store.getItem("isRegisteredForKMS") === null) {
kony.push.register(configRegister);
}
} catch (err) {
kony.print("KMS Module" + JSON.stringify(err));
}
}
/*
 * @function regSuccessCallback
 * This function is registration success callback on successful registration of APS or GCM
 * this function is used to register to kony messaging  for push notification
*/
function regSuccessCallback(regId) {
try {
      alert("reg successful");
kony.store.setItem("isRegisteredForKMS", "true");
KMSPROP.deviceId = kony.os.deviceInfo().deviceid; // device id
KMSPROP.device_rec = kony.os.deviceInfo().deviceid; // unique key
var messagingSvc = KNYMobileFabric.getMessagingService();
messagingSvc.register(KMSPROP.osType, KMSPROP.deviceId, regId, KMSPROP.device_rec, successCallbackForSubscribe, failureCallbackForSubscribe);
function successCallbackForSubscribe(res) {
kony.application.dismissLoadingScreen();
kony.print(JSON.stringify(res));
}
function failureCallbackForSubscribe(err) {
kony.store.removeItem("isRegisteredForKMS");
kony.application.dismissLoadingScreen();
kony.print(JSON.stringify(err));
kony.print("Subscription Failed.");
}
} catch (err) {
kony.print("KMS Module" + JSON.stringify(err));
}
}
/*
 * @function regFailureCallback
 * This function is registration failure callback on failure registration of APS or GCM
 */
function regFailureCallback(errormsg) {
kony.print(errormsg);
kony.print("Registration Failed ");
}

/*
 * @function onlinePushNotificationCallback
 * This function is the callback for online push notification
 */
function onlinePushNotificationCallback(msg) {
try {
alert("push message received"+JSON.stringify(msg));
} catch (err) {
kony.print("KMS Module" + JSON.stringify(err));
}
}
/*
 * @function offlinePushNotificationCallback
 * This function is the callback for offline push notification
 */
function offlinePushNotificationCallback(msg) {
try {
alert("push message received"+JSON.stringify(msg));
} catch (err) {
kony.print("KMS Module" + JSON.stringify(err));
}
}

function unregSuccessCallback() {
kony.print("Unregisterd Succesfully!!");
}
function unregFailureCallback(errormsg) {
kony.print(" Unregistration Failed!!" + errormsg);
}

Note:  If you get package generation failed after selecting FCM in push notification, from firebase-> your apps, download the google-services.json file. Create a folder named "fcm" in project location->resources->mobile->native->android and paste the json file. Restart the visualizer and take build.