android - implementing push notification in ionic app -
i trying implement push notification in ionic app android. have followed step step guide http://docs.ionic.io/v1.0/docs/push-from-scratch. when running app on android phone, registered users listed in apps.ionic.io. user registration working fine. device registration not working. giving error cannot read property 'pushnotification' of undefined
this code @ top of app.js
angular.module('starter', ['ionic','ngcordova', 'ionic.service.core', 'ionic.service.push', 'starter.controllers', 'starter.services']) .config(['$ionicappprovider', function($ionicappprovider) { // identify app $ionicappprovider.identify({ // app id (from apps.ionic.io) server app_id: '', // public api key services use app api_key: '', // set app use development pushes // dev_push: true gcm_id: '' }); }])
here code in of controller
.controller('dashboardctrl', function($scope,$localstorage, wildfireservice, commonutilityservice,pushnotificationservice,$ionicpopup, $ionicloading) { pushnotificationservice.identifyuser(); pushnotificationservice.pushregister(); })
here services.js
.service('pushnotificationservice', function($q, $ionicuser, $ionicpush) { var pushnotificationservice = this; pushnotificationservice.identifyuser = function(){ var user = $ionicuser.get(); if(!user.user_id) { // set user_id here, or generate random one. user.user_id = $ionicuser.generateguid(); }; // add metadata user object. angular.extend(user, { name: 'technews', bio: 'hardcoded now' }); // identify user ionic user service $ionicuser.identify(user).then(function(){ //alert('identified user ' + user.name + '\n id ' + user.user_id); return true; }); }, pushnotificationservice.pushregister = function(){ // register ionic push service. parameters optional. $ionicpush.register({ canshowalert: true, //can pushes show alert on screen? cansetbadge: true, //can pushes update app icon badges? canplaysound: true, //can notifications play sound? canrunactionsonwake: true, //can run actions outside app, onnotification: function(notification) { // handle new push notifications here // console.log(notification); alert(notification); return true; } }); } })
can 1 tell me error or missing?
i have added these in index.html
<script src="lib/ionic/js/ionic.bundle.js"></script> <script src="lib/ngcordova/dist/ng-cordova.js"></script> <script src="lib/ionic-service-core/ionic-core.js"></script> <script src="lib/ionic-service-push/ionic-push.js"></script>
finally push notification working me. moved function call of pushregister controller identifyuser function. here new working code me. controller code
.controller('dashboardctrl', function($scope,$localstorage, wildfireservice, commonutilityservice,pushnotificationservice,$ionicpopup, $ionicloading) { pushnotificationservice.identifyuser(); })
here new services.js
.service('pushnotificationservice', function($q, $ionicuser, $ionicpush) { var pushnotificationservice = this; pushnotificationservice.identifyuser = function(){ var user = $ionicuser.get(); if(!user.user_id) { // set user_id here, or generate random one. user.user_id = $ionicuser.generateguid(); }; // add metadata user object. angular.extend(user, { name: 'technews', bio: 'hardcoded now' }); // identify user ionic user service $ionicuser.identify(user).then(function(){ //alert('identified user ' + user.name + '\n id ' + user.user_id); pushnotificationservice.pushregister(); return true; }); }, pushnotificationservice.pushregister = function(){ // register ionic push service. parameters optional. $ionicpush.register({ canshowalert: true, //can pushes show alert on screen? cansetbadge: true, //can pushes update app icon badges? canplaysound: true, //can notifications play sound? canrunactionsonwake: true, //can run actions outside app, onnotification: function(notification) { // handle new push notifications here // console.log(notification); alert(notification); return true; } }); } })
Comments
Post a Comment