I'm using cordova to create a hybrid app.
I am trying to push notification on Android using the plug-in below, but
Push notifications are not sent when the app is in the background.
If the app is foregrounded, it will be sent without any problems.
It is implemented almost the same as the reference site below.
Do you know why?
◎ Reference site
http://symfoware.blog68.fc2.com/blog-entry-1662.html
◎ Plug-in used
https://github.com/phonegap-build/PushPlugin
◎ Implementation code (executed below when device ready)
var pushNotification;
pushNotification=window.plugins.pushNotification;
if(device.platform=='android'||device.platform=='Android'){
pushNotification.register(
successHandler,
errorHandler, {
"senderID": 12345678, // Actually, you have set the correct ID.
"ecb": "onNotification"
});
} else{
// ios
pushNotification.register(
tokenHandler,
errorHandler, {
"badge": "true",
"sound": "true",
"alert": "true",
"ecb": "onNotificationAPN"
});
}
function successHandler(result){
console.log('result='+result);
}
function tokenHandler(result){
console.log('device token='+result);
}
function errorHandler(error){
console.log('error='+error);
}
function onNotificationAPN(event){
if(event.alert){
navigator.notification.alert(event.alert);
}
if(event.sound){
varsnd = new Media(event.sound);
snd.play();
}
if(event.badge){
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}
}
// Android and Amazon Fire OS
window.onNotification=function(e){
switch(e.event){
case 'registered':
if(e.regid.length>0){
console.log("regID(device token)="+e.regid);
}
break;
case 'message':
if(e.foreground){
alert(e.payload.default);
} else{
if(e.coldstart){
console.log("===message.background.coldstart===");
} else{
console.log("===message.background.non coldstart===");
}
}
break;
case 'error':
console.log('error');
break;
default:
break;
}
};
When AndroidManifest does not have the following permissions, it looks like a question.
What do you think?
<uses-permission android:name="android.permission.WAKE_LOCK"/>
© 2024 OneMinuteCode. All rights reserved.