android - Cordova local notification doesn't work while app is in background -
i'm developing android application using cordova , uses pushplugin receive force notifications server.
in particular, i'm doing test using pushplugin example.
i'm using cordova local notification plugin because want app shows local notification receives force notification.
the next code works , local notification appears when app in foreground.
i want local notification appear when app in background. possible? how can create work?
thanks in advance
<!doctype html> <html> <head> <title>com.phonegap.c2dm</title> </head> <body> <script type="text/javascript" charset="utf-8" src="cordova.js"></script> <script type="text/javascript" charset="utf-8" src="jquery_1.5.2.min.js"></script> <script type="text/javascript" src="pushnotification.js"></script> <script type="text/javascript"> var pushnotification; function ondeviceready() { $("#app-status-ul").append('<li>deviceready event received</li>'); document.addeventlistener("backbutton", function(e){ $("#app-status-ul").append('<li>backbutton event received</li>'); if( $("#home").length > 0){ // phone call new token each time. don't phone call reuse existing token. //pushnotification.unregister(successhandler, errorhandler); e.preventdefault(); navigator.app.exitapp(); } else{ navigator.app.backhistory(); } }, false); try{ pushnotification = window.plugins.pushnotification; $("#app-status-ul").append('<li>registering ' + device.platform + '</li>'); if (device.platform == 'android' || device.platform == 'android' || device.platform == 'amazon-fireos' ){ pushnotification.register(successhandler, errorhandler, {"senderid":"527085141383","ecb":"onnotification"}); } else { pushnotification.register(tokenhandler, errorhandler, {"badge":"true","sound":"true","alert":"true","ecb":"onnotificationapn"}); // obbligatorio! } } catch(err) { txt="there error on page.\n\n"; txt+="error description: " + err.message + "\n\n"; alert(txt); } } // fine ondeviceready function onnotificationapn(e) { if (e.alert) { $("#app-status-ul").append('<li>push-notification: ' + e.alert + '</li>'); // showing alert requires org.apache.cordova.dialogs plugin navigator.notification.alert(e.alert); } if (e.sound) { // playing sound requires org.apache.cordova.media plugin var snd = new media(e.sound); snd.play(); } if (e.badge) { pushnotification.setapplicationiconbadgenumber(successhandler, e.badge); } } function onnotification(e) { $("#app-status-ul").append('<li>event -> received:' + e.event + '</li>'); switch( e.event ){ case 'registered': if ( e.regid.length > 0 ) { $("#app-status-ul").append('<li>registered -> regid:' + e.regid + "</li>"); // gcm force server needs know regid before can force device // here might want send regid later use. console.log("regid = " + e.regid); } break; case 'message': // if flag set, notification happened while in foreground. // might want play sound user's attention, throw dialog, etc. var notificaok = function(){ console.log("ok"); } var notificako = function(){ console.log("ko"); } window.plugin.notification.local.add({id: 1, title: "product available", message: "nexus 6 in stock", smallicon: 'ic_dialog_email', icon: 'ic_launcher'}, notificaok, notificako); if (e.foreground){ $("#app-status-ul").append('<li>--inline notification--' + '</li>'); // on android soundname outside payload. // on amazon fireos custom attributes contained within payload var soundfile = e.soundname || e.payload.sound; // if notification contains soundname, play it. // playing sound requires org.apache.cordova.media plugin var my_media = new media("/android_asset/www/"+ soundfile); my_media.play(); } else{ // otherwise launched because user touched notification in notification tray. if (e.coldstart) $("#app-status-ul").append('<li>--coldstart notification--' + '</li>'); else $("#app-status-ul").append('<li>--background notification--' + '</li>'); } $("#app-status-ul").append('<li>message -> msg: ' + e.payload.message + '</li>'); //android $("#app-status-ul").append('<li>message -> msgcnt: ' + e.payload.msgcnt + '</li>'); //amazon-fireos $("#app-status-ul").append('<li>message -> timestamp: ' + e.payload.timestamp + '</li>'); break; case 'error': $("#app-status-ul").append('<li>error -> msg:' + e.msg + '</li>'); break; default: $("#app-status-ul").append('<li>event -> unknown, event received , not know is</li>'); break; } } function tokenhandler (result) { $("#app-status-ul").append('<li>token: '+ result +'</li>'); // ios force server needs know token before can force device // here might want send token later use. } function successhandler (result) { $("#app-status-ul").append('<li>success:'+ result +'</li>'); } function errorhandler (error) { $("#app-status-ul").append('<li>error:'+ error +'</li>'); } document.addeventlistener('deviceready', ondeviceready, true); </script> <div id="home"> <div id="app-status-div"> <ul id="app-status-ul"> <li>cordova pushnotification plugin demo</li> </ul> </div> </div> </body> </html>
then send force notification device next nodejs script:
var gcm = require('gcm').gcm; var apikey = "***"; var gcm = new gcm(apikey); var devregidtarget = "apa9...."; var message = { message: "text msg", registration_id : devregidtarget, title : 'title', msgcnt : '1', collapsekey : "msg1", soundname : 'beep.wav' }; message.timetolive = 3000; message.delaywhileidle = true; gcm.send(message, function(err, messageid){ if (err) { console.log("something has gone wrong!"); } else { console.log("sent message id: ", messageid); } });
i had similar experience in using pushplugin , localnotification.
for case, notification worked on background smallicon android white blank.
i spent whole night inspecting source of localnotification plugin turned out problem pushplugin itself. (i say, exact problem disconnection between 2 plugins) in source code of pushplugin, checks whether application in foreground or background.
if application in background, notification event not triggered cordova app pushplugin creates it's own local notification.
@override protected void onmessage(context context, intent intent) { log.d(tag, "onmessage - context: " + context); // extract payload message bundle extras = intent.getextras(); if (extras != null) { // if in foreground, surface payload, else post statusbar if (pushplugin.isinforeground()) { extras.putboolean("foreground", true); pushplugin.sendextras(extras); } else { extras.putboolean("foreground", false); // send notification if there message if (extras.getstring("message") != null && extras.getstring("message").length() != 0) { createnotification(context, extras); } } } }
so, if want utilize customised smallicon when application running in back.
i did quick hack overriding initial code to
public void createnotification(context context, bundle extras) { notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service); string appname = getappname(this); intent notificationintent = new intent(this, pushhandleractivity.class); notificationintent.addflags(intent.flag_activity_single_top | intent.flag_activity_clear_top); notificationintent.putextra("pushbundle", extras); pendingintent contentintent = pendingintent.getactivity(this, 0, notificationintent, pendingintent.flag_update_current); int defaults = notification.default_all; if (extras.getstring("defaults") != null) { seek { defaults = integer.parseint(extras.getstring("defaults")); } grab (numberformatexception e) {} } notificationcompat.builder mbuilder = new notificationcompat.builder(context) .setdefaults(defaults) .setsmallicon(getresourceid(context, "pushicon", "drawable", context.getpackagename())) .setlargeicon(bitmapfactory.decoderesource(context.getresources(), getresourceid(context, "icon", "drawable", context.getpackagename()))) .setwhen(system.currenttimemillis()) .setcontenttitle(extras.getstring("title")) .setticker(extras.getstring("title")) .setcontentintent(contentintent) .setautocancel(true); string message = extras.getstring("message"); if (message != null) { mbuilder.setcontenttext(message); } else { mbuilder.setcontenttext("<missing message content>"); } string msgcnt = extras.getstring("msgcnt"); if (msgcnt != null) { mbuilder.setnumber(integer.parseint(msgcnt)); } int notid = 0; seek { notid = integer.parseint(extras.getstring("notid")); } catch(numberformatexception e) { log.e(tag, "number format exception - error parsing notification id: " + e.getmessage()); } catch(exception e) { log.e(tag, "number format exception - error parsing notification id" + e.getmessage()); } mnotificationmanager.notify((string) appname, notid, mbuilder.build()); }
now, need set image named "pushicon" in drawable folder platforms/android/res/drawable/pushicon.png (i made utilize image "icon" big icon well)
if it's much of hassle, made git repo https://github.com/zxshinxz/pushplugin.git
cordova plugin add together https://github.com/zxshinxz/pushplugin.git
hope other programmer don't go through pain went through.
android cordova notifications push-notification cordova-plugins
No comments:
Post a Comment