简体   繁体   English

无法使用Cordova推送通知插件获取设备令牌

[英]Can't get device token using Cordova Push Notifications Plugin

I'm building a Push Notification app using Ionic framework, so I try following this sample app: https://github.com/hollyschinsky/PushNotificationSample 我正在使用Ionic框架构建推送通知应用程序,因此我尝试使用以下示例应用程序: https//github.com/hollyschinsky/PushNotificationSample

The problem is that when I try to run the sample in an Android device it doesn't retrive the device token. 问题是,当我尝试在Android设备中运行示例时,它不会检索设备令牌。 I replaced the senderId in the register function with the one from my own Google application, but it doesn't work. 我将注册表函数中的senderId替换为我自己的Google应用程序中的senderId,但它不起作用。

What am I doing wrong? 我究竟做错了什么?

Here are the versions that I'm using: 以下是我正在使用的版本:

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+

The problem I think is in here in notificationReceived function: 我认为这个问题在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));
        })
    }
});

EDIT: 编辑:

It turns out that the app does retrieves the device token, it just not displaing it on screen as it should. 事实证明,该应用程序确实检索了设备令牌,它只是不应该在屏幕上显示它应该。

I had to run the app on my device connected via USB and with ionic run android, and see how the app was running with adb logcat, then I saw a line that said "registrationId = APA91b..." that's the token. 我必须在我的设备上运行应用程序通过USB和离子运行android连接,看看应用程序如何运行adb logcat,然后我看到一行说“registrationId = APA91b ...”这是令牌。 I tested the notifications with the sendGCM.js file as indicated in the tutorial and it works. 我使用sendGCM.js文件测试了通知,如教程中所示,它可以工作。

But still it doesn't explains why it doesn't show the Token or the received notifications in the index page. 但它仍然没有解释为什么它没有在索引页面中显示令牌或收到的通知。 Is like the Token is always out of the controllers scope. 就像Token总是在控制器范围之外。

Any help? 有帮助吗?

It's because your didn't set push notifications properly. 这是因为你没有正确设置推送通知。

What you need to do now is remove all existing certificates in your member developer account and rebuild again. 您现在需要做的是删除成员开发人员帐户中的所有现有证书并重新重建。 If you try to debug it takes time. 如果你试图调试它需要时间。

You can check here if you want to see step by step how to create push notification using cordova push plugin. 如果您想逐步了解如何使用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-1/

http://blog.revivalx.com/2014/08/29/implement-push-notifications-for-android-and-ios-phonegap-part-2/ 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/ http://blog.revivalx.com/2014/11/14/implement-push-notifications-for-android-and-ios-phonegap-part-3/

Sorry not posting here all the steps because it's too long. 很抱歉没有在这里发布所有步骤,因为它太长了。 Feel free to ask. 随意问。

For push notification to work. 用于推送通知工作。 You need an actual device. 你需要一个实际的设备。 Emulators are not recommended. 不建议使用仿真器。 Connect your android/iOS phone and do. 连接你的android / iOS手机吧。 ionic run android.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM