PushNotification.register使用GCM未接收到注册ID(PushNotific

2019-10-20 13:36发布

我想pushNotification功能添加到我的Android app.I遵循了以下教程中的步骤: http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-应用/

当他们上面说的,我已经创造了谷歌开发者控制台中的项目,并获得该项目的ID。 现在我可以看到“为谷歌Android云通讯”为ON。 我一直在使用CLI科尔多瓦创建的项目在我的机器和下面的是我的JavaScript。 我使用的是Android 4.4奇巧。

我有,我得到的结果为“OK”的问题,但我仍在这里没有使用PhoneGap的建立,我没有得到注册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构建和使用科尔多瓦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)

Answer 1:

最后我做到了工作。 我上面的JavaScript代码是正确的。 主要的问题是最初我试图在Android模拟器中运行,但没有奏效。 它甚至没有我试着用真实的设备上运行。 我重设手机厂并安装我的应用程序一次,然后它工作最后。



文章来源: PushNotification.register using GCM is not receiving Registration Id