-1

Despite all tries and reviewing previous questions and solutions on SO, I cannot manage to get a registrationID from GCM.

I am using the pushplugin, and trying to build for Android using Cordova. App is successfully built but it seems the onNotificationGCM functions in the are never being called. Success handler is called. I use Genymotion Android simulator.

On github issues some say to attach the onNotification functions to window object. But I could not make this work too. Is there a general problem. Anybody using this plugin successfully?

What can be the reason for that? The trial here uses another cordova plugin different than the mentioned one above. Although I have tried the example in the above plugin's github repo, it did not work.

index.js:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    // "senderID":"273794328096"
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        console.log('Received Event: ' + id);
        if (device.platform == 'android' || device.platform == 'Android') {
            alert("Register called");
            window.GcmPushPlugin.register(app.successHandler, app.errorHandler, {
                "senderId":"273794328096",
                "jsCallback":"onNotificationGCM"
            });
        }
        else {
            alert("Register called");
            pushNotification.register(this.successHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});
        }
    },
    // result contains any message sent from the plugin call
    successHandler: function(result) {
        alert('Callback Success! Result = '+ result.gcm)
    },
    errorHandler:function(error) {
        alert("Error:" + error);
    },
};

function onNotificationGCM(notification) {
  console.log("Event Received: " + notification); // { "extra": {"url" : "someurl.js" } } 
}

index.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </head>
    <body>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>
    </body>
</html>
sçuçu
  • 2,960
  • 2
  • 33
  • 60
  • 1
    Suggest that you try a GCM client sample code from GitHub – BNK Aug 22 '15 at 04:10
  • I have tried three of them. but they do not work. – sçuçu Aug 22 '15 at 17:33
  • @Huey added relevant code now. – sçuçu Aug 22 '15 at 18:03
  • Cannot understand why downvote. Is it as if there are some persons just downvote passing by. Anyway it's a beautiful life. – sçuçu Aug 24 '15 at 00:07
  • You can refer to my answer [here](http://stackoverflow.com/questions/32110963/how-to-make-asynctask-wait-untill-gcm-broadcast-is-received/32111549#32111549). If it works for you, I'll add as answer at this question. – BNK Aug 24 '15 at 00:20
  • @BNK Sorry but my codebase is JavaScript on Cordova 5.x platform, not Java. Thanks. – sçuçu Aug 24 '15 at 00:23

1 Answers1

0

Example code

var Project = {};

var app = {
    initialize: function() {
        this.bindEvents();
    },

    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },

    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },

    receivedEvent: function(id) {
        app.registerDevice();
    },

    registerDevice: function(){
        if (window.cordova && window.cordova.platformId=='android'){
            try{
                var pushNotification = window.plugins.pushNotification;
                pushNotification.register(
                    app.successPN,
                    app.errorPN,
                    {
                        "senderID": "727700427600",
                        "ecb": "Project.Notification"
                    }
                );
            }
            catch (ex){
                // window.alert('Error (message): ' + ex.message);
                // window.alert('Error (stack): ' + ex.stack);
            }
            console.log("regID = " + localStorage['pioneer.device.regid']);
        }
    },
    successPN: function (result){
        // window.alert('PN Success: ' + result);
    },
    errorPN: function (result){
        // window.alert('PN Error: ' + result);
    }
};

app.initialize();

Project.Notification

Project.Notification = function(e){
    // Pioneer.alert(JSON.stringify(e), 'alert');
    switch( e.event )
    {
        case 'registered':
            // process reg id
            break;                
        case 'message':
            // print message
            break;
        case 'error':
            // window.alert("Notification error: " + e.msg);
            break;
        default:
            // window.alert("Notification - Unknown event");
            break;
    }
};
Ajoy
  • 1,838
  • 3
  • 30
  • 57
  • No way. It is not working. ı only see the output alert of the sucessPN saying OK. Also to clear I have put some alerts in Project.Notification cases: for each like: `window.alert("registerId: " + e.regid + " " + e.event.regid); window.alert("Notification message: " + " " + e + " " + e.message + " " + e.event.message); window.alert("Notification error: " + e.msg); window.alert("Notification - Unknown event");` – sçuçu Aug 22 '15 at 18:02
  • Problem solved! I am now testing app on the real Android device rather than simulator. – sçuçu Aug 24 '15 at 00:10