2

我一直在关注这个示例来让 android pushNotifications(使用 GCM)在 android 模拟器上工作。在$cordovaPush.register(config)我得到 Ok 作为回应之后。但它从不运行我的回调 [ $scope.$on('$cordovaPush:notificationReceived']。结果我从来没有得到我的注册ID。

我创建了一个谷歌 API 项目。我在调用 config.senderID 时使用了该项目 ID $cordovaPush.register(config)

我还在模拟器中注册了一个 gmail 帐户。

我想我有2个问题。

1.是否可以在安卓模拟器上获取(和注册)推送通知?

  1. 为什么我没有收到调用我的回调的 $cordovaPush:notificationReceived 事件?

    app.controller('AppCtrl', function($scope, $cordovaPush, $cordovaDialogs, $cordovaMedia, $cordovaToast, ionPlatform, $http) { $scope.notifications = [];

    // call to register automatically upon device ready
    ionPlatform.ready.then(function (device) {
        $scope.register();
    });
    
    
    // Register
    $scope.register = function () {
        var config = null;
    
        if (ionic.Platform.isAndroid()) {
            config = {
                "senderID": "12834957xxxx" 
            };
        }
    
        $cordovaPush.register(config).then(function (result) {
            console.log("Register success " + result);
    
            $cordovaToast.showShortCenter('Registered for push notifications');
            $scope.registerDisabled=true;
    
        }, function (err) {
            console.log("Register error " + err)
        });
    }
    
    
    $scope.$on('$cordovaPush:notificationReceived', function (event, notification) {
        console.log(JSON.stringify([notification]));
        if (ionic.Platform.isAndroid()) {
            handleAndroid(notification);
        }
    
    });
    
    // Android Notification Received Handler
    function handleAndroid(notification) {
        // ** NOTE: ** You could add code for when app is in foreground or not, or coming from coldstart here too
        //             via the console fields as shown.
        console.log("In foreground " + notification.foreground  + " Coldstart " + notification.coldstart);
        if (notification.event == "registered") {
            $scope.regId = notification.regid;
            storeDeviceToken("android");
        }
        else if (notification.event == "message") {
            $cordovaDialogs.alert(notification.message, "Push Notification Received");
            $scope.$apply(function () {
                $scope.notifications.push(JSON.stringify(notification.message));
            })
        }
        else if (notification.event == "error")
            $cordovaDialogs.alert(notification.msg, "Push notification error event");
        else $cordovaDialogs.alert(notification.event, "Push notification handler - Unprocessed Event");
    }
    
4

4 回答 4

4

修复比看起来简单得多。我从:

$scope.$on('$cordovaPush:notificationReceived', function (event, notification) {

至:

$scope.$on('pushNotificationReceived', function (event, notification) {

调试时,我在 ng-cordova.js 上注意到了这一点:

angular.module('ngCordova.plugins.push', [])
    .factory('$cordovaPush', ['$q', '$window', '$rootScope', function ($q, $window, $rootScope) {
        return {
            onNotification: function (notification) {
                $rootScope.$apply(function () {
                    $rootScope.$broadcast('pushNotificationReceived', notification);
                });
            },

这意味着它正在为“pushNotificationReceived”进行广播。不是文档中的“notificationReceived” 。

于 2015-02-24T00:28:33.763 回答
2

我也有同样的问题。已经在 ngCordova github 上报道过,但还没有回复。

我设法修复它。我知道如果您使用 angular,这不是一个好的解决方案,但这是我能够捕获寄存器 ID 的唯一方法。

  1. 在配置对象中,您必须指定'ecb'

    var androidConfig = {
        "senderID": "388573974286",
        "ecb": "function_to_be_called"
    };
    
  2. 将功能放在控制器外部:



        window.function_to_be_called = function (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;
        }
    };

于 2015-02-23T18:50:44.007 回答
0

即使按照你说的更新事件名称,我也有另一个问题,上面写着:processmessage failed: message: jjavascript:angular.element(document.queryselector('[ng-app]')).injector().get('$cordovapush').onnotification({"event":"registered"...

当 Angular 找不到“ng-app”并返回“未定义”时会发生这种情况angular.element(document.querySelector('[ng-app]'))

所以要解决这个问题,你可以ecb像这样手动设置你的'':

angular.element(document.body).injector().get('$cordovaPush').onNotification

感谢这个离子论坛条目

于 2015-02-24T19:17:02.017 回答
0

请从http://ngcordova.com/docs/install/在 ng-cordova-master\dist\ng-cordova.js中更新您的 ng-cordova.js 在 lib/ng -cordova.js

于 2015-03-27T07:01:44.067 回答