Is it possible to send push notification in ionic framework. any code is available
You can also do it with a plugin like this one: https://github.com/appfeel/cordova-push-notifications#automatic-installation (you will have to implement server code to register user device token and send notifications on each case too)
cordova plugin add cordova-push-notifications
Then in your javascript code:
var pushNotification, successHandler, tokenHandler, errorHandler;
document.addEventListener("deviceready", function () {
var token;
pushNotification = window.plugins.pushNotification;
if (pushNotification) {
// Token usually saved in window.localStorage see https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
token = "recover_saved_token_here";
startPushNotifications(token);
}
});
function startPushNotifications(token) {
var registerOptions;
if (device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos") {
registerOptions = {
"senderID":"replace_with_sender_id",
"token": "the_user_device_token_empty_first_time"
"ecb":"onNotificationGCM" // This function is called as window function
};
} else if (device.platform == 'iOS') {
successHandler = tokenHandler; // We need to save the token
registerOptions = {
"badge":"true",
"sound":"true",
"alert":"true",
"ecb":"onNotificationAPN" // This function is called as window function
};
} else {
// Not implemented
}
pushNotification.register(successHandler, errorHandler, registerOptions);
}
successHandler = function (result) {
console.log("Push success callback: ", result);
};
errorHandler = function (error) {
console.log("Push error callback: ", error);
};
And you need to implement the callbacks. Android and Amazon ones:
window.onNotificationGCM = function onNotificationGCM(data) {
var soundfile,
mediaSound;
switch (e.event) {
case 'registered':
if (e.regid.length > 0) {
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use in token parameter.
// Token usually saved in window.localStorage see https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
console.log("User device token (android/amazon): " + e.regid);
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if (parseInt(e.foreground, 0)) {
// on Android soundname is outside the payload.
// On Amazon FireOS all custom attributes are contained within payload
soundfile = e.soundname || e.payload.soundmMedia;
// Requires cordova Media plugin
if (soundfile && Media) {
mediaSound = new Media('/android_asset/www/sounds/' + soundfile);
mediaSound.play();
}
} else { // otherwise we were launched because the user touched a notification in the notification tray.
if (e.coldstart) {
// coldstart notification: the app was running in background (exited with home button or app switched)
} else {
// background notification: the app was not running (exited with back button)
}
}
// android only: e.payload.msgcnt
// amazon-fireos only: e.payload.timeStamp
break;
case 'error':
console.log("Error: " + e.msg);
break;
default:
console.log("Unknown event: " + e.event);
break;
}
};
And iOS ones:
tokenHandler = function tokenHandler(result) {
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
// Token usually saved in window.localStorage see https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
console.log("User device token (iOS): ", JSON.stringify(result));
};
window.onNotificationAPN = function onNotificationAPN(e) {
var mediaSound;
if (parseInt(e.foreground, 0) && e.sound && Media) {
// playing a sound also requires the org.apache.cordova.media plugin
mediaSound = new Media("sounds/" + e.sound);
mediaSound.play();
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.