繁体   English   中英

在ngCordova / Ionic中推送通知,只需“确定”即可

[英]Push notifications in ngCordova/Ionic, getting just “OK”

我第一次尝试使用此示例代码为Android创建混合应用程序。 我正在测试以查看通知是否可以正常工作,并且出现两个错误:

  1. 您应该得到的result对象不包含任何内容,只是可以

  2. 我得到$rootScope is not defined ,我应该在哪里定义?

我的代码是

var app = angular.module('starter', ['ionic', 'ngCordova']);

app.run(function($cordovaPush) {

  var androidConfig = {
    "senderID": "84xxxxxxxx",
  };

  document.addEventListener("deviceready", function(){
    $cordovaPush.register(androidConfig).then(function(result) {
      // Success
      console.log("OK, result is " + result);
    }, function(err) {
      // Error
      console.log("NOT OK");
    })

    $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
      switch(notification.event) {
        case 'registered':
          if (notification.regid.length > 0 ) {
            alert('registration ID = ' + notification.regid);
          }
          break;

        case 'message':
          // this is the actual push notification. its format depends on the data model from the push server
          alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
          break;

        case 'error':
          alert('GCM error = ' + notification.msg);
          break;

        default:
          alert('An unknown GCM event has occurred');
          break;
      }
    });


    // WARNING: dangerous to unregister (results in loss of tokenID)
    $cordovaPush.unregister(options).then(function(result) {
      // Success!
    }, function(err) {
      // Error
    })

  }, false);
});

像这样在.run()中包含$rootScope

app.run(function($cordovaPush,$rootScope) {

result register响应的result对您没有任何帮助。如果注册成功,您将收到一个registered事件,您需要的是注册ID,您将在这里收到此事件作为notification.regid

 case 'registered':
          if (notification.regid.length > 0 ) {
            alert('registration ID = ' + notification.regid);
          }
          break;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM