PushNotification.register使用GCM不接收注册ID [英] PushNotification.register using GCM is not receiving Registration Id

查看:316
本文介绍了PushNotification.register使用GCM不接收注册ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想pushNotification功能添加到我的Andr​​oid app.I已按照下面的教程步骤:<一href=\"http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/\" rel=\"nofollow\">http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

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

我有,我得到的结果是OK的问题,但是,我在这里还是没有使用PhoneGap的建设,我没有得到注册ID。我已经创建了谷歌在开发控制台项目提到的项目ID。下面code是我的index.js:

  VAR应用= {
        //应用程序构造
        初始化:功能(){
        this.bindEvents();
        },
        //绑定事件监听器
        //
        //绑定所需的启动任何事件。常见的事件是:
        //'负荷','deviceready,离线和在线。
        bindEvents:功能(){
        document.addEventListener(deviceready',this.onDeviceReady,FALSE);
        },
        // deviceready事件处理程序
        //
        //的这的范围是该事件。为了称之为receivedEvent
        //函数中,我们必须显式地称之为app.receivedEvent(...);
        onDeviceReady:功能(){
        警报(该设备就可以使用了);
        app.receivedEvent('deviceready');
        警报(发送项目的ID GCM服务器与GCM登记);
        VAR pushNotification = window.plugins.pushNotification;
        pushNotification.register(app.successHandler,app.errorHandler,{senderID:776989336731,欧洲央行:app.onNotificationGCM});
        },
        //更新DOM上接收到的事件
        receivedEvent:功能(ID){
        警报(输入在接收到的事件);
        变种parentElement =的document.getElementById(ID);
        VAR listeningElement = parentElement.querySelector('倾听');
        VAR receivedElement = parentElement.querySelector('收到');        listeningElement.setAttribute('风格','显示:无;');
        receivedElement.setAttribute(风格,显示:块;');        的console.log('接收到的事件:'+ id)的;
        },
        //结果包含在插件调用发送的任何消息
        successHandler:功能(结果){
        警报('回调成功!结果='+结果)
        },
        的ErrorHandler:功能(错误){
        警报(错误);
        },
        onNotificationGCM:功能(E){
        警报(在onNotificationGCM+ e.event);
        开关(e.event)
        {
        情况下注册:
        如果(e.regid.length大于0)
        {
        的console.log(REGID+ e.regid);
        警报('注册ID ='+ e.regid);
        }
        打破;        案消息:
        //这是实际的推送通知。其格式取决于从推送服务器的数据模型
        警报(消息='+ e.message +'msgcnt ='+ e.msgcnt);
        打破;        案错误:
        警报('GCM错误='+ e.msg);
        打破;        默认:
        警报('未知GCM事件已经发生');
        打破;
        }
        }        };

我在网上搜索了很多,大家都在说的问题得到了解决,但没有身体已经给予适当的回答。注意:在这里我没有使用目前的PhoneGap构建和使用科尔多瓦CLI添加pushPlugin

我发现下面的日志中logcat的:

 插件管理线程Warniing:EXEC()调用PushPlugin寄存器挡在主线程。
  GCMBroadcastReceiver的onReceive:com.ggole.android.gcm.intent.RETRY
                       GCM IntentService类:com.plugin.gcm.GCMIntentService
                       GCMBaseIntentService:AcquiringWeblock
handleRegistration:registrationId = NULL,差错= SERVICE_NOT_AVAILABLE,未注册= NULL
注册错误:SERVICE_NOT_AVAILABLE
调度注册重试,回退= 10730(12000)


解决方案

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

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)

解决方案

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.

这篇关于PushNotification.register使用GCM不接收注册ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆