Ionic Framework PushPlugin: onNotificationGMC is not fired and cannot obtain regID
Asked Answered
G

2

1

I believe that this problem looks like this question:

Cordova Push Plugin: onNotificationGMC is not fired and cannot obtain regID

But I'm using Ionic Framework. I follow this tutorial in making PushProcessingService:

http://intown.biz/2014/04/11/android-notifications/

//factory for processing push notifications.
angular.module('starter.pusher', [])
        .factory('PushProcessingService', function(MyService) {
            function onDeviceReady() {
                console.info('NOTIFY  Device is ready.  Registering with GCM server');
                alert('NOTIFY  Device is ready.  Registering with GCM server');
                //register with google GCM server
                var pushNotification = window.plugins.pushNotification;
                pushNotification.register(gcmSuccessHandler, gcmErrorHandler, {'senderID': 'myappid', 'ecb': 'onNotificationGCM'});
            }
            function gcmSuccessHandler(result) {
                console.info('NOTIFY  pushNotification.register succeeded.  Result = ' + result);
                alert('NOTIFY  pushNotification.register succeeded.  Result = ' + result)
            }
            function gcmErrorHandler(error) {
                console.error('NOTIFY  ' + error);
            }
            return {
                initialize: function() {
                    console.info('NOTIFY  initializing');
                    document.addEventListener('deviceready', onDeviceReady, false);
                },
                registerID: function(regid) {
                    //Insert code here to store the user's ID on your notification server. 
                    //You'll probably have a web service (wrapped in an Angular service of course) set up for this.  
                    //For example:
                    MyService.registerNotificationID(regid).then(function(response) {
                        if (response.data.Result) {
                            console.info('NOTIFY  Registration succeeded');
                        } else {
                            console.error('NOTIFY  Registration failed');
                        }
                    });
                },
                //unregister can be called from a settings area.
                unregister: function() {
                    console.info('unregister')
                    var push = window.plugins.pushNotification;
                    if (push) {
                        push.unregister(function() {
                            console.info('unregister success')
                        });
                    }
                }
            }
        });


// ALL GCM notifications come through here. 
function onNotificationGCM(e) {
    console.log('EVENT -> RECEIVED:' + e.event + '');
    alert('EVENT -> RECEIVED:' + e.event + '');
    switch (e.event)
    {
        case 'registered':
            if (e.regid.length > 0)
            {
                console.log('REGISTERED with GCM Server -> REGID:' + e.regid + '');
                alert(e.regid);
                //call back to web service in Angular.  
                //This works for me because in my code I have a factory called
                //      PushProcessingService with method registerID
                var elem = angular.element(document.querySelector('[ng-app]'));
                var injector = elem.injector();
                var myService = injector.get('PushProcessingService');
                myService.registerID(e.regid);
            }
            break;

        case 'message':
            // if this flag is set, this notification happened while we were in the foreground.
            // you might want to play a sound to get the user's attention, throw up a dialog, etc.
            if (e.foreground)
            {
                //we're using the app when a message is received.
                console.log('--INLINE NOTIFICATION--' + '');

                // if the notification contains a soundname, play it.
                //var my_media = new Media('/android_asset/www/'+e.soundname);
                //my_media.play();
                alert(e.payload.message);
            }
            else
            {
                // otherwise we were launched because the user touched a notification in the notification tray.
                if (e.coldstart)
                    console.log('--COLDSTART NOTIFICATION--' + '');
                else
                    console.log('--BACKGROUND NOTIFICATION--' + '');

                // direct user here:
                window.location = '#/tab/dash';
            }

            console.log('MESSAGE -> MSG: ' + e.payload.message + '');
            console.log('MESSAGE: ' + JSON.stringify(e.payload));
            break;

        case 'error':
            console.log('ERROR -> MSG:' + e.msg + '');
            alert('ERROR -> MSG:' + e.msg + '');
            break;

        default:
            console.log('EVENT -> Unknown, an event was received and we do not know what it is');
            alert('EVENT -> Unknown, an event was received and we do not know what it is');
            break;
    }
}

But the onNotificationGCM(e) callback seems not to be working. I've moved it inside the factory, but the problem persists. I call the function in my app.js:

app.run(function($ionicPlatform, PushProcessingService) {
            try {
                PushProcessingService.initialize();
            } catch (err) {
                alert(err);
            }
            $ionicPlatform.ready(function() {
                // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
                // for form inputs)
                if (window.cordova && window.cordova.plugins.Keyboard) {
                    cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                }
                if (window.StatusBar) {
                    // org.apache.cordova.statusbar required
                    StatusBar.styleDefault();
                }
            });
        })

Please help me on solving this. Because I've been stuck for few days. Thank you!! :)

Granadilla answered 12/2, 2015 at 3:4 Comment(0)
G
0

I finally try to use ngCordova Push Notification plugin as described here:

http://ngcordova.com/docs/plugins/pushNotifications/

Everything works well.

To be noted: The plugin won't work in browser nor emulator. It works only in real device, in my case, Android device.

I hope this helps people who face the same problem as I had.

Granadilla answered 13/2, 2015 at 3:15 Comment(5)
Push notification never work with simulator, it will work with device only both ios and android.. but it is possible to notification in android simulator , for that to have to enable : see this google.co.in/…Melessa
I've checked your reference. Thank you.. :)Granadilla
Please do you have exampleDuo
Where I should put the API keys on ngCordovaDuo
Have you read the link I refer above? It clearly tells you how to configure your app (iOS and Android).. :)Granadilla
M
2

are device ready function getting called? are you adding device ready listener?

do like this:

document.addListener("deviceready" function(){
        var pushNotification = window.plugins.pushNotification;
            pushNotification.register(gcmSuccessHandler, gcmErrorHandler,  {'senderID': 'myappid', 'ecb': 'onNotificationGCM'});
        }
        function gcmSuccessHandler(result) {
            console.info('NOTIFY  pushNotification.register succeeded.  Result = ' + result);
            alert('NOTIFY  pushNotification.register succeeded.  Result = ' + result)
        }
        function gcmErrorHandler(error) {
            console.error('NOTIFY  ' + error);
        }
 });
Melessa answered 12/2, 2015 at 8:34 Comment(1)
Thanks for the response. I call it inside the initialize() function: document.addEventListener('deviceready', onDeviceReady, false); FYI, I've tried with real Android device, but still not working. Previously, I had tried this plugin with jQuery, everything is fine.Granadilla
G
0

I finally try to use ngCordova Push Notification plugin as described here:

http://ngcordova.com/docs/plugins/pushNotifications/

Everything works well.

To be noted: The plugin won't work in browser nor emulator. It works only in real device, in my case, Android device.

I hope this helps people who face the same problem as I had.

Granadilla answered 13/2, 2015 at 3:15 Comment(5)
Push notification never work with simulator, it will work with device only both ios and android.. but it is possible to notification in android simulator , for that to have to enable : see this google.co.in/…Melessa
I've checked your reference. Thank you.. :)Granadilla
Please do you have exampleDuo
Where I should put the API keys on ngCordovaDuo
Have you read the link I refer above? It clearly tells you how to configure your app (iOS and Android).. :)Granadilla

© 2022 - 2024 — McMap. All rights reserved.