javascript - Ionic push notification Application -
i working on ionic project have implement push notification practically have no idea again app going used in corporate environment . kindly suggest.
hello rigel, first of have install 4 plugins that. 1)cordova plugin add https://github.com/phonegap-build/pushplugin // notification 2)cordova plugin add cordova-plugin-device // on device ready call 3)cordova plugin add cordova-plugin-dialogs // notification dialog 4)cordova plugin add cordova-plugin-media // notification sound
and generate api key , senderid throw developer console.and senderid past in bellow code. , e.regid pass on server.
<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) { // 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; $("#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":"661780372179","ecb":"onnotification"}); // required! } else { 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>'); // 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); } } // handle gcm notifications android 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 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>'); // 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 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> <div id="home"> <div id="app-status-div"> <ul id="app-status-ul"> <li>cordova pushnotification plugin demo</li> </ul> </div> </div>
Comments
Post a Comment