android - When click status bar Notification (Phonegap PushPlugin) The application is launched but does not detect coldstart state -
i developing android application in eclipse , have installed phonegap pushplugin, works perfectly, notifications received , launched in status bar , open application when touched.
the problem comes when application off (meaning coldstate) , application launched not run action inside "if ( e.coldstart )"
i wrote code based on following example published in official documentation of plugin:
function onnotificationgcm(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 push server needs know regid before can push 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. if (e.foreground) { $("#app-status-ul").append('<li>--inline notification--' + '</li>'); // if notification contains soundname, play it. var my_media = new media("/android_asset/www/" + e.soundname); 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>'); $("#app-status-ul").append('<li>message -> msgcnt: ' + e.payload.msgcnt + '</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; } }
this code on main page of application opens when tap notification , show labels "inline notification" , "background notification" whichever applicable
not supposed place within
if (e.coldstart) { $("#app-status-ul").append('<li>--coldstart notification--' + '</li>'); }
should executed if app comes being turned off?
would appreciate cooperation because application should open page distinct index.html when notifications arrive , staying in index.html
here i'll edit question place text fragment in original plugin documentation refers variable "coldstart":
finally, should exit app hitting button home page, may still receive notification. touching notification in notification tray relaunch app , allow process notification (coldstart). in case coldstart flag set on incoming event.
also add own code opens new page when receiving push-notification:
var app = { // application constructor initialize: function() { this.bindevents(); }, // bind event listeners // // bind events required on startup. common events are: // 'load', 'deviceready', 'offline', , 'online'. bindevents: function() { document.addeventlistener('deviceready', this.ondeviceready, false); }, // deviceready event handler // // scope of 'this' event. in order call 'receivedevent' // function, must explicity call 'app.receivedevent(...);' ondeviceready: function() { app.receivedevent(); }, // update dom on received event receivedevent: function() { var pushnotification = window.plugins.pushnotification; pushnotification.register(app.successhandler, app.errorhandler,{"senderid":"my sender id","ecb":"app.onnotificationgcm"}); }, // result contains message sent plugin call successhandler: function(result) { alert('callback success! result = '+result) }, errorhandler:function(error) { alert(error); }, onnotificationgcm: function(e) { switch( e.event ) { case 'registered': if ( e.regid.length > 0 ) { console.log("regid " + e.regid); window.localstorage.setitem("gcmid", e.regid); } break; case 'message': // if flag set, notification happened while in foreground. // might want play sound user's attention, throw dialog, etc. if ( e.foreground ) { alert('foreground message = '+e.message+' msgcnt = '+e.msgcnt+' room= '+e.payload.room_msg+' lat = '+lat_r+' lng = '+lng_r); top.location.href="chat.html?idu=notapply&room_snd=notapply&roomname_snd=" + e.payload.room_msg + "&lat_snd=" + lat_r + "&lng_snd=" + lng_r + "&msg_snd=" + e.message; } else { // otherwise launched because user touched notification in notification tray. if ( e.coldstart ) { alert('foreground message = '+e.message+' msgcnt = '+e.msgcnt+' room= '+e.payload.room_msg+' lat = '+lat_r+' lng = '+lng_r); top.location.href="chat.html?idu=notapply&room_snd=notapply&roomname_snd=" + e.payload.room_msg + "&lat_snd=" + lat_r + "&lng_snd=" + lng_r + "&msg_snd=" + e.message; } else { alert('foreground message = '+e.message+' msgcnt = '+e.msgcnt+' room= '+e.payload.room_msg+' lat = '+lat_r+' lng = '+lng_r); top.location.href="chat.html?idu=notapply&room_snd=notapply&roomname_snd=" + e.payload.room_msg + "&lat_snd=" + lat_r + "&lng_snd=" + lng_r + "&msg_snd=" + e.message; } } break; case 'error': alert('gcm error = '+e.msg); break; default: alert('an unknown gcm event has occurred'); break; } } };
solve problem temporarily adding additional payload indicates activity should open page distinct index.html, although solution not make "e.coldstart" passed "true," add function in pushhandlerativity.java:
after:
private void forcemainactivityreload() { packagemanager pm = getpackagemanager(); intent launchintent = pm.getlaunchintentforpackage(getapplicationcontext().getpackagename()); startactivity(launchintent); }
before:
private void forcemainactivityreload() { packagemanager pm = getpackagemanager(); intent launchintent = pm.getlaunchintentforpackage(getapplicationcontext().getpackagename()); bundle extras = getintent().getextras(); launchintent.putextra("room_msg", extras.getstring("room_msg")); startactivity(launchintent); }
of course in function generates notification use adddatawithkeyvalue function add additional payload, main activity add following:
bundle extras = getintent().getextras(); string message = extras.getstring("room_msg"); if(message != null){ super.loadurl("file:///android_asset/www/chat.html?", 10000); }else{ super.loadurl("file:///android_asset/www/index.html", 10000); }
my english not good. ensure pushplugin working good, should have test case in java make sure plugin run correct or have mistake. when touch in notification, pushhandleractivity call, call function:
private void processpushbundle(boolean ispushpluginactive) { bundle extras = getintent().getextras(); if (extras != null) { bundle originalextras = extras.getbundle("pushbundle"); originalextras.putboolean("foreground", false); originalextras.putboolean("coldstart", !ispushpluginactive); pushplugin.sendextras(originalextras); } }
to identify app in background or app closed( in case fired coldstart-->true)
edit 1: coldstart true when app closed(not in background) , click notification lauch app. when app in background coldstart still false. can hack code change variable 'ispushpluginactive'=true in above function.
Comments
Post a Comment