无法使用Cordova Push Notifications Plugin获取设备令牌

时间:2015-04-29 14:41:38

标签: android cordova push-notification ionic-framework cordova-plugins

我正在使用Ionic框架构建推送通知应用,因此我尝试使用此示例应用:https://github.com/hollyschinsky/PushNotificationSample

问题在于,当我尝试在Android设备中运行该示例时,它不会检索设备令牌。我将注册表函数中的senderId替换为我自己的Google应用程序中的senderId,但它不起作用。

我做错了什么?

以下是我使用的版本:

Ionic    --version 1.3.20
Cordova  --version 5.0.0
Phonegap --version 4.2.0-0.24.2
Android device: HTC One S
Android version: 4.1.1 HTC Sense 4+

我认为问题出现在notificationReceived函数中:

// Register
$scope.register = function () {
    var config = null;

    if (ionic.Platform.isAndroid()) {
        config = {
            "senderID": "1034029444859" // REPLACE THIS WITH YOURS FROM GCM CONSOLE - also in the project URL like: https://console.developers.google.com/project/434205989073
        };
        //alert("El senderID es: " + senderID);
    }
    else if (ionic.Platform.isIOS()) {
        config = {
            "badge": "true",
            "sound": "true",
            "alert": "true"
        }
    }

    $cordovaPush.register(config).then(function (result) {
        console.log("Register success " + result);

        $cordovaToast.showShortCenter('Registered for push notifications ' + config.senderID);
        $scope.registerDisabled=true;
        // ** NOTE: Android regid result comes back in the pushNotificationReceived, only iOS returned here
        if (ionic.Platform.isIOS()) {
            $scope.regId = result;
            storeDeviceToken("ios");
        }
    }, function (err) {
        console.log("Register error " + err)
    });
}

// Notification Received
$scope.$on('$cordovaPush:notificationReceived', function (event, notification) {
    console.log(JSON.stringify([notification]));
    if (ionic.Platform.isAndroid()) {
        handleAndroid(notification);
    }
    else if (ionic.Platform.isIOS()) {
        handleIOS(notification);
        $scope.$apply(function () {
            $scope.notifications.push(JSON.stringify(notification.alert));
        })
    }
});

修改

事实证明,该应用程序确实检索了设备令牌,它不会在屏幕上显示它应该。

我必须在我的设备上运行应用程序,通过USB和离子运行android连接,看看应用程序是如何运行adb logcat的,然后我看到一行表示" registrationId = APA91b ...& #34;这是令牌。我使用sendGCM.js文件测试了通知,如教程中所示,它可以工作。

但它仍然没有解释为什么它没有在索引页面中显示令牌或收到的通知。就像Token总是在控制器范围之外。

任何帮助?

1 个答案:

答案 0 :(得分:3)

这是因为你没有正确设置推送通知。

您现在需要做的是删除会员开发者帐户中的所有现有证书,然后重新进行重建。如果你试图调试它需要时间。

如果您想逐步了解如何使用cordova push插件创建推送通知,可以在此处查看。

http://blog.revivalx.com/2014/08/29/implement-push-notifications-for-android-and-ios-phonegap-part-1/

http://blog.revivalx.com/2014/08/29/implement-push-notifications-for-android-and-ios-phonegap-part-2/

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

抱歉不要在这里发布所有步骤,因为它太长了。随意问。