0

我在尝试使用 ngCordova 和 $cordovaPush 注册我的 GCM regid 时遇到了麻烦。

我无法在控制器中检索我的 regid 以通过 $http 将其发送到我的 API:

var gcmToken;
gcmNotificationHandler = function(e) {
    if (e.event === "registered") {
        gcmToken = e.regid;
        console.log(gcmToken);
    }
};
var aTonAvis = angular.module('aTonAvis', ['ngCordova']);
aTonAvis.value('serverUrl', "http://atonavis.local/");
aTonAvis.controller('RegisterCtrl', function($scope, $cordovaPush,
    $cordovaDevice, $http, serverUrl) {
    var gcmConfig = {
        "senderID": "00000000000"
    };
    var iosConfig = {
        "badge": "true",
        "sound": "true",
        "alert": "true",
    };
    gcmConfig.ecb = "gcmNotificationHandler";
    iosConfig.ecb = "iosNotificationHandler";
    var configHandler = function() {
        if ($cordovaDevice.getPlatform().toLowerCase() ===
            'android' || $cordovaDevice.getPlatform() ===
            'amazon-fireos') {
            return gcmConfig;
        } else {
            return iosConfig;
        }
    };
    this.registered = false;
    document.addEventListener("deviceready", function onDeviceReady() {
        $cordovaPush.register(configHandler()).then(function(
            result) {
            console.log("Inside register " + gcmToken);
            var pushToken;
            if ($cordovaDevice.getPlatform().toLowerCase() ===
                'ios') pushToken = result;
            else pushToken = gcmToken;
            $http.post(serverUrl + 'api/setdevice', {
                token: pushToken,
                device: $cordovaDevice.getPlatform(),
                version: $cordovaDevice.getVersion(),
                model: $cordovaDevice.getModel()
            }).success(function(data, status,
                headers, config) {
                this.registered = true;
            });
        }, function(err) {
            console.log("Registering Error : " + err);
        });
    });
});

.then 函数在 gcmNotificationHandler 之前触发,因此我的 gcmToken 未定义,这是我在日志中得到的内容:

Inside register : undefined app.js:41
APA91bEzVUk3T1T6WpIsEHOq43hCh_pZeBPjRDPSPxV2j6VjVW-KcUepbmf6snaCiqGvYp3H_XYHIXQdbVtvMF3t-NtoZJaJzV9FkNtUlutuWYs5XPAZ-H1ixQnAyhTO6fAPDMn7Ef5f5HgBR9fgWpmXc0u_xBM4yKvoXCnVXEG18IZV2hvY app.js:6

我不知道我在这里做错了什么,有人可以帮忙吗?

4

2 回答 2

0

在运行应用程序之前,您需要安装这两个 cordova 插件。

设备cordova插件:https ://github.com/apache/cordova-plugin-device

cordova plugin add https://github.com/apache/cordova-plugin-device

控制台科尔多瓦插件:https ://github.com/apache/cordova-plugin-console

cordova plugin add https://github.com/apache/cordova-plugin-console

http://blog.revivalx.com/2014/11/14/implement-push-notifications-for-android-and-ios-phonegap-part-3/

于 2014-12-08T10:47:04.173 回答
0

这是我使用 ngCordova 和 PushPlugin 的代码。regid 为 android 的事件“pushNotificationReceived”提供相同的回调。这是我在该事件的回调中使用的代码,用于从 GCM 接收 regid:

检查 (notification.event === 'registered') 是最重要的。

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

  //console.log("result: " + JSON.stringify(event));
  console.log("result: " + JSON.stringify(notification));        

  console.log('Success: Inside the push notification received callback with this payload JSON' + notification);

  if(( platform == 'android' || platform == 'Android' ) && notification.regid && (notification.event === 'registered'))
  {
    var googDevToken = notification.regid;
    console.log(googDevToken);
  }

});
于 2015-01-29T19:20:44.713 回答