ngCordova在控制器内部获得GCM regid

时间:2014-12-05 11:02:50

标签: angularjs cordova push-notification phonegap-pushplugin

我在尝试使用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

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

2 个答案:

答案 0 :(得分:0)

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

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

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

控制台cordova插件: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/

答案 1 :(得分:0)

这是我的代码,它使用了带有PushPlugin的ngCordova。 regid来自事件的同一回调" pushNotificationReceived'对于Android。以下是我在该回调中使用的代码,用于从GCM接收regid:

检查(notification.event ==='已注册')是最重要的。

$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);
  }

});