无法在Android中获取推送通知以使用ngCordova

时间:2015-01-28 21:18:04

标签: javascript angularjs cordova ionic

我很难获得推送通知(使用ngCordova插件)。我完全按照网站上记录的示例代码进行了跟踪:http://ngcordova.com/docs/plugins/pushNotifications/

(唯一的区别是我没有设备监听器,而是我的代码在ionicPlatform.ready监听器内。)

这是我的代码:

angular.module('myApp', ['ionic', 'ngCordova'])
.run(function($ionicPlatform, $rootScope, $state, $cordovaPush) {
  $ionicPlatform.ready(function() {
    var config = {
      "senderID": "myID100001000"
    };

    $cordovaPush.register(config).then(function(result) {
      alert(result);
    }, function(err) {
      alert(err);
    })      
  }); 

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

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

当我的应用程序启动时,我确实得到了#34; OK"警报,所以我知道它成功通过$ cordovaPush.register调用。但是,我期待得到一个"注册"通知事件,之后,但我从未收到通知。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:8)

解决方案在评论中,但这需要一个正确的答案。

首先,只要您传递registerOK回调始终会返回senderID,但如果永远不会调用$cordovaPush:notificationReceived事件(可能需要几秒钟),这个ID可能是错误的。

必须使用项目编号,而不是项目ID。

要获取该号码,请转到API Console,选择项目,然后您将进入概述页面。在这个页面的顶部,你会看到这样的东西:

Project ID: your-project-id        Project Number: 0123456789

只需复制并使用项目编号,一切都应该有效。

答案 1 :(得分:1)

我经历了很多这方面的事情,我发现,目前有两个版本的cordova push插件:

两者都受到ngCordova的支持,但只记录了已弃用的版本。

弃用版本为 $ cordovaPush 而较新的是 $ cordovaPushV5 ,他们有完全不同的方法。

对我来说问题是我下载了cordova-plugin-push并尝试使用ngCordova网站上的旧文档来实现它。

代码是:

 /*
         * Non deprecated version of Push notification events
         */
        function registerV5() {

            $ionicLoading.show({
                template: '<ion-spinner></ion-spinner>'
            });

            if (ionic.Platform.is('browser')) {
                alert("You are running on broswer, please switch to your device. Otherwise you won't get notifications");
                $ionicLoading.hide();
                return;
            }

            /**
             * Configuration doc:
             * https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#pushnotificationinitoptions
             */
            var GCM_PROJECT_ID = 'xxxxxx';

            $cordovaPushV5.initialize({
                "android": {
                    "clearNotifications": false,
                    "senderID" : GCM_PROJECT_ID
                }
            });

            $cordovaPushV5.register().then(function (deviceToken) {
                console.log("Successfully registered", deviceToken);

                $scope.data.deviceToken = deviceToken;

                // Below code required to configure $cordovaPushV5 notifications emitter. 
                // Don't pass function it's not handler.
                $cordovaPushV5.onNotification();
                $cordovaPushV5.onError();

                $ionicLoading.hide();
            }, function (error) {
                console.log("Failed to registered");
                console.log("error object : ", error);
                $ionicLoading.hide();
            });
        }

        $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data) {
            console.log("notification received");
            console.log("data object: ", data);

            var foreground = data.additionalData.foreground || false;
            var threadID = data.additionalData.payload.threadID || '';
            var group = data.additionalData.payload.group || false;

            if (foreground) {
                // Do something if the app is in foreground while receiving to push - handle in app push handling
                console.log('Receive notification in foreground');
            } else {
                // Handle push messages while app is in background or not started
                console.log('Receive notification in background');
                // Open FB messanger app when user clicks notification UI when app is in background.
                if (typeof data.additionalData.coldstart != "undefined" && data.additionalData.coldstart == false)
                    if (!group)
                        // Open FB Messenger of specific user chat window
                        window.open('fb-messenger://user/' + threadID, '_system', 'location=no');
                    else
                        // Open FB Messenger of specific group chat window
                        window.open('fb-messenger://groupthreadfbid/' + threadID, '_system', 'location=no');                
            }
        });

        $rootScope.$on('$cordovaPushV5:errorOccurred', function(event, error) {
            console.log("notification error occured");
            console.log("event object: ", event);
            console.log("error object: ", error);
        });

有关此github文章的更多信息:https://github.com/driftyco/ng-cordova/issues/1125(此处的代码)和本文中的内容:https://github.com/yafraorg/yafra/wiki/Blog-Ionic-PushV5