1

我相信这个问题是这样的问题:离子框架PushPlugin:onNotificationGMC不会触发,并且不能获得的RegID

Cordova Push Plugin: onNotificationGMC is not fired and cannot obtain regID

但我使用离子框架。我按照使得PushProcessingService本教程:

http://intown.biz/2014/04/11/android-notifications/

//factory for processing push notifications. 
angular.module('starter.pusher', []) 
     .factory('PushProcessingService', function(MyService) { 
      function onDeviceReady() { 
       console.info('NOTIFY Device is ready. Registering with GCM server'); 
       alert('NOTIFY Device is ready. Registering with GCM server'); 
       //register with google GCM server 
       var pushNotification = window.plugins.pushNotification; 
       pushNotification.register(gcmSuccessHandler, gcmErrorHandler, {'senderID': 'myappid', 'ecb': 'onNotificationGCM'}); 
      } 
      function gcmSuccessHandler(result) { 
       console.info('NOTIFY pushNotification.register succeeded. Result = ' + result); 
       alert('NOTIFY pushNotification.register succeeded. Result = ' + result) 
      } 
      function gcmErrorHandler(error) { 
       console.error('NOTIFY ' + error); 
      } 
      return { 
       initialize: function() { 
        console.info('NOTIFY initializing'); 
        document.addEventListener('deviceready', onDeviceReady, false); 
       }, 
       registerID: function(regid) { 
        //Insert code here to store the user's ID on your notification server. 
        //You'll probably have a web service (wrapped in an Angular service of course) set up for this. 
        //For example: 
        MyService.registerNotificationID(regid).then(function(response) { 
         if (response.data.Result) { 
          console.info('NOTIFY Registration succeeded'); 
         } else { 
          console.error('NOTIFY Registration failed'); 
         } 
        }); 
       }, 
       //unregister can be called from a settings area. 
       unregister: function() { 
        console.info('unregister') 
        var push = window.plugins.pushNotification; 
        if (push) { 
         push.unregister(function() { 
          console.info('unregister success') 
         }); 
        } 
       } 
      } 
     }); 


// ALL GCM notifications come through here. 
function onNotificationGCM(e) { 
    console.log('EVENT -> RECEIVED:' + e.event + ''); 
    alert('EVENT -> RECEIVED:' + e.event + ''); 
    switch (e.event) 
    { 
     case 'registered': 
      if (e.regid.length > 0) 
      { 
       console.log('REGISTERED with GCM Server -> REGID:' + e.regid + ''); 
       alert(e.regid); 
       //call back to web service in Angular. 
       //This works for me because in my code I have a factory called 
       //  PushProcessingService with method registerID 
       var elem = angular.element(document.querySelector('[ng-app]')); 
       var injector = elem.injector(); 
       var myService = injector.get('PushProcessingService'); 
       myService.registerID(e.regid); 
      } 
      break; 

     case 'message': 
      // if this flag is set, this notification happened while we were in the foreground. 
      // you might want to play a sound to get the user's attention, throw up a dialog, etc. 
      if (e.foreground) 
      { 
       //we're using the app when a message is received. 
       console.log('--INLINE NOTIFICATION--' + ''); 

       // if the notification contains a soundname, play it. 
       //var my_media = new Media('/android_asset/www/'+e.soundname); 
       //my_media.play(); 
       alert(e.payload.message); 
      } 
      else 
      { 
       // otherwise we were launched because the user touched a notification in the notification tray. 
       if (e.coldstart) 
        console.log('--COLDSTART NOTIFICATION--' + ''); 
       else 
        console.log('--BACKGROUND NOTIFICATION--' + ''); 

       // direct user here: 
       window.location = '#/tab/dash'; 
      } 

      console.log('MESSAGE -> MSG: ' + e.payload.message + ''); 
      console.log('MESSAGE: ' + JSON.stringify(e.payload)); 
      break; 

     case 'error': 
      console.log('ERROR -> MSG:' + e.msg + ''); 
      alert('ERROR -> MSG:' + e.msg + ''); 
      break; 

     default: 
      console.log('EVENT -> Unknown, an event was received and we do not know what it is'); 
      alert('EVENT -> Unknown, an event was received and we do not know what it is'); 
      break; 
    } 
} 

但onNotificationGCM(E)的回调似乎并不奏效。我已将它移到工厂内部,但问题依然存在。我在我的app.js中调用函数:

app.run(function($ionicPlatform, PushProcessingService) { 
      try { 
       PushProcessingService.initialize(); 
      } catch (err) { 
       alert(err); 
      } 
      $ionicPlatform.ready(function() { 
       // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
       // for form inputs) 
       if (window.cordova && window.cordova.plugins.Keyboard) { 
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
       } 
       if (window.StatusBar) { 
        // org.apache.cordova.statusbar required 
        StatusBar.styleDefault(); 
       } 
      }); 
     }) 

请帮我解决这个问题。因为我被困了几天。谢谢!! :)

回答

0

我终于尝试使用ngCordova推送通知插件如下所述:

http://ngcordova.com/docs/plugins/pushNotifications/

一切运作良好。

要注意插件不会在浏览器也不模拟器工作。它只适用于在真实设备,在我的情况下,Android设备。

我希望这可以帮助那些面对同样问题的人。

+0

推送通知从不与模拟器一起工作,它只能与设备一起使用ios和android ..但它可以在android模拟器中进行通知,以便启用:请参阅https://www.google.co.in /url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0CDIQtwIwAw&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DePR1GZeATeE&ei=PM_dVMHBNoOpuQSBsICwBw&usg=AFQjCNE8IENVtBTnuiLsybRVGJExpfYx_Q&sig2=CqZ5S3D3RHbtL8uTJs4nUQ&bvm=bv.85970519 ,d.c2E – Amar1989 2015-02-13 10:18:31

+0

我检查了您的参考。谢谢.. :) – asubanovsky 2015-02-13 10:38:42

+0

请问你有没有例子 – 2015-11-04 16:44:16

2

是设备就绪函数被调用?你在添加设备准备好的监听器吗?

这样做:

document.addListener("deviceready" function(){ 
     var pushNotification = window.plugins.pushNotification; 
      pushNotification.register(gcmSuccessHandler, gcmErrorHandler, {'senderID': 'myappid', 'ecb': 'onNotificationGCM'}); 
     } 
     function gcmSuccessHandler(result) { 
      console.info('NOTIFY pushNotification.register succeeded. Result = ' + result); 
      alert('NOTIFY pushNotification.register succeeded. Result = ' + result) 
     } 
     function gcmErrorHandler(error) { 
      console.error('NOTIFY ' + error); 
     } 
}); 
+0

感谢您的回复。我在initialize()函数内调用它:document.addEventListener('deviceready',onDeviceReady,false);仅供参考,我已经尝试过使用真正的Android设备,但仍然无法正常工作。以前,我曾用jQuery试过这个插件,一切都很好。 – asubanovsky 2015-02-12 09:24:29