我正在尝试向我的Android应用程序添加pushNotification功能。我已按照以下教程中的步骤操作:http ://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-应用/
正如他们上面所说,我已经在Google开发人员控制台中创建了一个项目,并获取了该项目的ID。现在,我可以看到“ Android的Google云消息传递”处于启用状态。我已经使用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)
终于,我成功了。我上面的javascript代码是正确的。主要问题是最初我尝试在android仿真器中运行,但没有成功。即使我尝试使用真实设备也无法运行。我重置了手机的出厂设置,并再次安装了我的应用,然后终于可以使用了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句