PushNotification.register using GCM is not receivi

2019-08-02 19:52发布

问题:

I am trying to add pushNotification feature to my Android app.I have followed the steps in the following tutorial: http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

As they said above, I have created a project in Google dev console and get the project id. Now I could be able to see "Google cloud messaging for Android" is ON. I have created the project in my machine using Cordova cli and the below is my JavaScript. I am using Android 4.4 Kitkat.

I have a problem that I got the result as 'OK' but I did not get registration id, here I am still not using PhoneGap build. I have created the project in Google dev console noted the project Id. The below code is my 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(...);' 
        onDeviceReady: function() { 
        alert("The device is ready to use"); 
        app.receivedEvent('deviceready'); 
        alert("Sending project id to GCM server to register with the gcm"); 
        var pushNotification = window.plugins.pushNotification; 
        pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"776989336731","ecb":"app.onNotificationGCM"}); 
        }, 
        // Update DOM on a Received Event 
        receivedEvent: function(id) { 
        alert("Entered in the received event"); 
        var parentElement = document.getElementById(id); 
        var listeningElement = parentElement.querySelector('.listening'); 
        var receivedElement = parentElement.querySelector('.received'); 

        listeningElement.setAttribute('style', 'display:none;'); 
        receivedElement.setAttribute('style', 'display:block;'); 

        console.log('Received Event: ' + id); 
        }, 
        // result contains any message sent from the plugin call 
        successHandler: function(result) { 
        alert('Callback Success! Result = '+result) 
        }, 
        errorHandler:function(error) { 
        alert(error); 
        }, 
        onNotificationGCM: function(e) { 
        alert("In the onNotificationGCM " + e.event); 
        switch( e.event ) 
        { 
        case 'registered': 
        if ( e.regid.length > 0 ) 
        { 
        console.log("Regid " + e.regid); 
        alert('registration id = '+e.regid); 
        } 
        break; 

        case 'message': 
        // this is the actual push notification. its format depends on the data model from the push server 
        alert('message = '+e.message+' msgcnt = '+e.msgcnt); 
        break; 

        case 'error': 
        alert('GCM error = '+e.msg); 
        break; 

        default: 
        alert('An unknown GCM event has occurred'); 
        break; 
        } 
        } 

        };

I searched the web a lot, everyone is saying problem got solved but no body has given proper answer. Note: here I am not using PhoneGap Build currently and added the pushPlugin by using Cordova cli.

I found the following logs in logcat:

PluginManager thread Warniing: exec() call to PushPlugin register blocked in the main thread.
  GCMBroadcastReceiver onReceive: com.ggole.android.gcm.intent.RETRY
                       GCM IntentService Class: com.plugin.gcm.GCMIntentService
                       GCMBaseIntentService: AcquiringWeblock


handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
Registration error: SERVICE_NOT_AVAILABLE
Scheduling registration retry, backoff = 10730 (12000)

回答1:

Finally i made it to work. My above javascript code is correct. the main problem is initially I tried to run in the android emulator but it did not work. It did not run even i tried with real device. I reset the phone factory and installed my app again then it works finally.