Cordova 无法从GCM获取注册ID
尽管进行了所有的尝试并回顾了之前的问题和解决方案,但我无法从GCM获得注册ID 我正在使用,并尝试使用Cordova为Android构建。应用程序已成功构建,但中的onNotificationGCM函数似乎从未被调用。调用成功处理程序。我使用Genymotion Android模拟器 在github问题上,有人说要将onNotification函数附加到窗口对象。但是我也不能让它工作。这是一个普遍的问题。有人成功使用这个插件吗 原因是什么? 这里的试验使用了另一个不同于上面提到的cordova插件。虽然我在上面插件的github repo中尝试了这个例子,但它没有起作用 index.js:Cordova 无法从GCM获取注册ID,cordova,google-cloud-messaging,phonegap-pushplugin,Cordova,Google Cloud Messaging,Phonegap Pushplugin,尽管进行了所有的尝试并回顾了之前的问题和解决方案,但我无法从GCM获得注册ID 我正在使用,并尝试使用Cordova为Android构建。应用程序已成功构建,但中的onNotificationGCM函数似乎从未被调用。调用成功处理程序。我使用Genymotion Android模拟器 在github问题上,有人说要将onNotification函数附加到窗口对象。但是我也不能让它工作。这是一个普遍的问题。有人成功使用这个插件吗 原因是什么? 这里的试验使用了另一个不同于上面提到的cordova插
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(...);'
// "senderID":"273794328096"
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
console.log('Received Event: ' + id);
if (device.platform == 'android' || device.platform == 'Android') {
alert("Register called");
window.GcmPushPlugin.register(app.successHandler, app.errorHandler, {
"senderId":"273794328096",
"jsCallback":"onNotificationGCM"
});
}
else {
alert("Register called");
pushNotification.register(this.successHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});
}
},
// result contains any message sent from the plugin call
successHandler: function(result) {
alert('Callback Success! Result = '+ result.gcm)
},
errorHandler:function(error) {
alert("Error:" + error);
},
};
function onNotificationGCM(notification) {
console.log("Event Received: " + notification); // { "extra": {"url" : "someurl.js" } }
}
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
</html>
你好,世界
app.initialize();
示例代码
var Project = {};
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
receivedEvent: function(id) {
app.registerDevice();
},
registerDevice: function(){
if (window.cordova && window.cordova.platformId=='android'){
try{
var pushNotification = window.plugins.pushNotification;
pushNotification.register(
app.successPN,
app.errorPN,
{
"senderID": "727700427600",
"ecb": "Project.Notification"
}
);
}
catch (ex){
// window.alert('Error (message): ' + ex.message);
// window.alert('Error (stack): ' + ex.stack);
}
console.log("regID = " + localStorage['pioneer.device.regid']);
}
},
successPN: function (result){
// window.alert('PN Success: ' + result);
},
errorPN: function (result){
// window.alert('PN Error: ' + result);
}
};
app.initialize();
项目通知
Project.Notification = function(e){
// Pioneer.alert(JSON.stringify(e), 'alert');
switch( e.event )
{
case 'registered':
// process reg id
break;
case 'message':
// print message
break;
case 'error':
// window.alert("Notification error: " + e.msg);
break;
default:
// window.alert("Notification - Unknown event");
break;
}
};
建议您尝试一个来自GitHubI的GCM客户端示例代码,我已经尝试了其中的三个。“但它们不起作用。”休伊现在添加了相关代码。我不明白为什么要投否决票。好像有一些人刚刚投了反对票。不管怎样,这是一种美好的生活。你可以参考我的答案。如果它对你有效,我会补充这个问题的答案。不可能。它不起作用。ı只能看到SuccessPN的输出警报说OK。同样为了澄清,我在项目中放置了一些警报。通知案例:对于每一个,比如:
window.alert(“registerId:+e.regid+”+e.event.regid);window.alert(“通知消息:”+“”+e+“”+e.message+“”+e.event.message);window.alert(“通知错误:+e.msg”);窗口警报(“通知-未知事件”)代码>问题已解决!我现在正在真正的Android设备上测试应用程序,而不是在模拟器上。