使用GCM的PushNotification.register未收到注册ID

时间:2014-07-25 12:06:04

标签: android cordova push-notification phonegap-plugins google-cloud-messaging

我正在尝试将pushNotification功能添加到我的Android应用中。我已按照以下教程中的步骤操作:http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

如上所述,我在Google开发者控制台中创建了一个项目,并获得了项目ID。现在我能够看到"谷歌的云消息传递Android和#34;是ON。我使用Cordova cli在我的机器上创建了项目,下面是我的JavaScript。我使用的是Android 4.4 Kitkat。

我有一个问题,我得到了结果' OK'但我没有获得注册ID,这里我仍然没有使用PhoneGap构建。我在Google开发控制台中创建了项目,注意到项目ID。下面的代码是我的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; 
        } 
        } 

        };

我在网上搜索了很多,每个人都说问题得到了解决,但没有人给出正确答案。注意:这里我目前没有使用PhoneGap Build,并使用Cordova cli添加了pushPlugin。

我在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 个答案:

答案 0 :(得分:1)

最后我开始工作了。我上面的javascript代码是正确的。最初的主要问题是 我试图在Android模拟器中运行,但它不起作用。即使我尝试使用真实设备,它也没有运行。我重置手机工厂并再次安装我的应用程序然后它最终工作。