android - Phonegap PushNotification to open a specific app page -
i doing pushnotification both android/ios.i have used phonegap push-plugin https://github.com/phonegap-build/pushplugin, seems work great.
i receiving message on device problem when click on received message goes app index.html page.but want open someother page eg home.html when clicked message , in home.html showing message.
how achieve this?
myphonegapactivity.java
package com.test; import org.apache.cordova.droidgap; import android.os.bundle; public class myphonegapactivity extends droidgap { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); super.setintegerproperty("splashscreen", r.drawable.splash); super.setintegerproperty("loadurltimeoutvalue", 60000); super.loadurl("file:///android_asset/www/index.html", 10000); } }
index.js
<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) { // call new token each time. don't call reuse existing token. //pushnotification.unregister(successhandler, errorhandler); e.preventdefault(); navigator.app.exitapp(); } else { navigator.app.backhistory(); } }, false); try { pushnotification = window.plugins.pushnotification; if (device.platform == 'android' || device.platform == 'android') { $("#app-status-ul").append('<li>registering android</li>'); pushnotification.register(successhandler, errorhandler, {"senderid":"661780372179","ecb":"onnotificationgcm"}); // required! } else { $("#app-status-ul").append('<li>registering ios</li>'); pushnotification.register(tokenhandler, errorhandler, {"badge":"true","sound":"true","alert":"true","ecb":"onnotificationapn"}); // required! } } catch(err) { txt="there error on page.\n\n"; txt+="error description: " + err.message + "\n\n"; alert(txt); } } // handle apns notifications ios function onnotificationapn(e) { if (e.alert) { $("#app-status-ul").append('<li>push-notification: ' + e.alert + '</li>'); navigator.notification.alert(e.alert); } if (e.sound) { var snd = new media(e.sound); snd.play(); } if (e.badge) { pushnotification.setapplicationiconbadgenumber(successhandler, e.badge); } } // handle gcm notifications android 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; } } function tokenhandler (result) { $("#app-status-ul").append('<li>token: '+ result +'</li>'); // ios push server needs know token before can push 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>
you can accomplish this,
step 1 : open gcmintentservice.java file
check method onmessage
in method find here passing intent myphonegapactivity.java ...now have pass information.... passing information intent
check line
intent notificationintent = new intent(context, myphonegapactivity.class); //here pass information notificationintent.putextra ("url",url);
can see above have passed information url ...for complete code see below
protected void onmessage(context context, intent intent) { log.d(tag, "onmessage - context: " + context); // extract payload message bundle extras = intent.getextras(); if (extras != null) { boolean foreground = this.isinforeground(); extras.putboolean("foreground", foreground); if (foreground){ pushhandleractivity.sendtoapp(extras); }else{ string message = extras.getstring("message"); string title = extras.getstring("title"); notification notif = new notification(android.r.drawable.btn_star_big_on, message, system.currenttimemillis() ); notif.flags = notification.flag_auto_cancel; notif.defaults |= notification.default_sound; notif.defaults |= notification.default_vibrate; string url = "notify"; intent notificationintent = new intent(context, myphonegapactivity.class); //here pass information notificationintent.putextra ("url",url); notificationintent.addflags(intent.flag_activity_single_top); pendingintent contentintent = pendingintent.getactivity(context, 0, notificationintent, 0); notif.setlatesteventinfo(context, title, message, contentintent); string ns = context.notification_service; notificationmanager mnotificationmanager = (notificationmanager) context.getsystemservice(ns); mnotificationmanager.notify(1, notif); } } }
step 2:
in myphonegapactivity.java
package com.test; import org.apache.cordova.droidgap; import android.os.bundle; public class myphonegapactivity extends droidgap { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); super.setintegerproperty("splashscreen", r.drawable.splash); super.setintegerproperty("loadurltimeoutvalue", 60000); bundle extras = getintent().getextras(); string message = extras.getstring("url"); if(message == "notify"){ super.loadurl("file:///android_asset/www/home.html", 10000); }else{ super.loadurl("file:///android_asset/www/index.html", 10000); } } }
thats when click notification load home.html file:-)
Comments
Post a Comment