我想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)