Can't get device token using Cordova Push Notifications plugin

I am creating a Push Notification app using Ionic framework, so I am trying to follow this example app: https://github.com/hollyschinsky/PushNotificationSample

The problem is that when I try to run the sample on an android device, it doesn't retrieve the device token. I replaced senderId in register function with one from my own google app, but it doesn't work.

What am I doing wrong?

Here are the versions 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'm thinking about is here in the 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 application retrieves the device token, but simply does not preempt it on the screen as it should.

I needed to run the app on my USB connected device and using android ion launcher and see how the app works with adb logcat and then I saw a line that said "registrationId = APA91b ..." that's the token, I tested notifications with sendGCM.js file as mentioned in the tutorial and it works.

But still it doesn't explain why it doesn't show the token or received notifications on the index page. It is like the token always goes out of the scope of the controllers.

Any help?

+3


source to share


2 answers


This is because you have not configured the push notifications properly.

Now you need to remove all existing certificates in your contributor's developer account and rebuild again. If you are trying to debug it takes time.

Here you can check if you want to see step by step how to create push notifications using the 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, do not post all the steps here because it is too long. Feel free to ask.

+3


source


For push notification to work. You really need a device. Emulators are not recommended. Connect your android / iOS phone and done.ionic run android.



0


source







All Articles