3

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?

David Prieto
  • 2,239
  • 4
  • 32
  • 51
  • can you console.log(JSON.stringify(result)) ? what do you get ? – Karan Kumar Apr 29 '15 at 17:22
  • I tried to run the emulator with 'ionic emulate android --livereload' to see the console.log and it displayed that "there was a network error (http://172.16.16.50:8100)". Any suggestions? – David Prieto Apr 29 '15 at 18:22
  • umm, I believe push notifications functionality should be tested on a real device and not an emulator. – mehany May 23 '15 at 13:11
  • @David I am at the same point of finding here too, but then how did you retrieve the ID and submitted it to your server? – mehany May 23 '15 at 13:13
  • @Emm I had the device connected to the PC and I ran **adb logcat** for debugging. The result of the registration function is buried in there. FYI I gave up on this example and made my own push notification functions based on the examples in the documentation (http://ngcordova.com/docs/plugins/pushNotifications/). And push notifications does work in Android emulator, but not in iOS emulator. – David Prieto May 24 '15 at 03:14
  • The registration Id is however not the same as the registration token. Did you figure out how to get the actual registration token? – shadowcursor Sep 17 '15 at 00:29
  • @mohamed.ahmed yes, I just stoped following tutorials and wrote the whole thing on my own following ngCordova's basic instructions. – David Prieto Sep 17 '15 at 14:22

2 Answers2

3

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.

Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • I followed the tutorial, but I couldn't figure out where to put the **Sender key** and **device registration** id in the simplepush.php file for Android (I have never use PHP before), and I don't see how that examples throws the device Token so you can use it. Plus I have a second problem, when I use `ionic run android` it says "LAUNCH SUCCESS" but nothing happens in the device. – David Prieto Apr 30 '15 at 19:17
0

For push notification to work. You need an actual device. Emulators are not recommended. Connect your android/iOS phone and do. ionic run android.

Karan Kumar
  • 2,655
  • 19
  • 36
  • And also don't set up live reload for push notification. I'm guessing it will run into CORS issues. – Karan Kumar Apr 29 '15 at 19:21
  • I said that I ran it on an HTC Android device, it doesn't show the Token. How can I see the console.log using an actual device instead of an emulator? – David Prieto Apr 29 '15 at 19:28
  • When device is connected. Type adb logcat at terminal. You'll get the device logs. – Karan Kumar Apr 29 '15 at 19:30
  • When I use ionic run android it says "LAUNCH SUCCESS" but nothing happens in the device. There is a question already for that but it doesn't have an usefull answer: http://stackoverflow.com/questions/25281711/ionic-run-android-seems-to-work-but-the-app-is-not-launched-on-the-phone-why – David Prieto Apr 30 '15 at 19:18