简体   繁体   中英

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

The problem is that when I try to run the sample in an Android device it doesn't retrive the device token. I replaced the senderId in the register function with the one from my own Google application, but it doesn't work.

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:

// 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. I tested the notifications with the sendGCM.js file as indicated in the tutorial and it works.

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.

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.

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/

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. ionic run android.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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