【问题标题】:Android PushNotifications with PushPlugin on emulator在模拟器上使用 PushPlugin 的 Android PushNotifications
【发布时间】:2015-02-23 15:47:44
【问题描述】:

我一直在关注this sample 以获得在 android 模拟器上运行的 android pushNotifications(使用 GCM)。 在$cordovaPush.register(config) 之后,我得到了 Ok 作为回复。但它永远不会运行我的回调 [$scope.$on('$cordovaPush:notificationReceived']。 因此,我永远不会得到我的注册 ID。

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

我还在模拟器中注册了一个 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");
    }
    

【问题讨论】:

    标签: android push-notification ionic-framework phonegap-pushplugin ngcordova


    【解决方案1】:

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

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

    到:

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

    debugging 我在 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”进行广播。不是 documented 的“notificationReceived”。

    【讨论】:

    • 在我的情况下,要遵循 android 中的 devgirl.org/2014/12/16/… 教程,我需要将这一行注释 $scope.$apply(function () { into handleAndroid function
    • 太烦人了,他们把那部分留在了文档中,仍然没有修复这个简单的行,没有它,激活推送通知的过程本身就令人沮丧......谢谢你的回答!你拯救了这一天。 :)
    【解决方案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;
            }
        };
    
    

    【讨论】:

    • 我遇到了类似的问题,它适用于控制器内的所有内容,但是当我将实现移动到 .factory 时它停止工作。您的解决方案有效地将函数 window.function_to_be_called 放在工厂内。我不明白为什么会这样,但 $scope.$on('$cordovaPush:notificationReceived', function(event, notification) 不会。
    【解决方案3】:

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

    当 Angular 找不到 'ng-app' 并返回 'undefined' angular.element(document.querySelector('[ng-app]'))

    时会发生这种情况

    所以要解决这个问题,您可以像这样手动设置“ecb”:

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

    感谢ionic forum entry

    【讨论】:

    • 你有最新版本的cordovaPush吗? [2.4.0]
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 2013-10-31
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多