簡體   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