【发布时间】:2015-02-07 20:16:45
【问题描述】:
这个问题很长而且很复杂,但我希望有人能帮帮我。我正在编写一个需要发送和接收推送通知的 Phonegap 应用程序。我想为此使用 Azure 服务总线,以便可以部署到多个移动平台。现在我只是在测试Android。我已经设置了我的 Google 和 Azure 帐户,并且下面的代码可以正常工作。当我在我的设备上部署我的应用程序时,onGcmNotification 被触发并且我从谷歌获得了一个 regid。然后,我使用提供的 regid 和模板进行注册,之后我收到一个警报(“Registered With Hub!”),所以我认为一切正常。但是,通过 Azure 调试控制台进行测试时,我无法获得任何推送通知。见下图。它只是一直告诉我“未找到所选平台的注册”。我部署的应用程序上没有弹出任何内容,并且永远不会触发 onGcmNotification。我错过了什么?还有其他方法可以完成我想要做的事情吗?
var GCM_SENDER_ID = 'XXXXXXXXXXXXX';
var MOBILE_SERVICE_URL = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var MOBILE_SERVICE_APP_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var mobileServiceClient;
var pushNotification;
var app = {
initialize: function () {
this.bindEvents();
},
bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady, true);
},
onDeviceReady: function () {
angular.element(document).ready(function () {
mobileServiceClient = new WindowsAzure.MobileServiceClient(MOBILE_SERVICE_URL, MOBILE_SERVICE_APP_KEY);
// Create a pushNotification (from the PushPlugin).
pushNotification = window.plugins.pushNotification;
// Platform-specific registrations.
if (device.platform == 'android' || device.platform == 'Android') {
// Register with GCM for Android apps.
pushNotification.register(successHandler, errorHandler,
{
"senderID": GCM_SENDER_ID,
"ecb": "onGcmNotification"
});
} else if (device.platform === 'iOS') {
// Register with APNS for iOS apps.
pushNotification.register(tokenHandler, errorHandler,
{
"badge": "false",
"sound": "false",
"alert": "true",
"ecb": "onApnsNotification"
});
}
angular.bootstrap(document, ['app']);
});
},
};
// Handle a GCM notification.
function onGcmNotification(e) {
switch (e.event) {
case 'registered':
// Handle the registration.
if (e.regid.length > 0) {
console.log("gcm id " + e.regid);
if (mobileServiceClient) {
// Create the integrated Notification Hub client.
var hub = new NotificationHub(mobileServiceClient);
// Template registration.
var template = "{ \"data\" : {\"message\":\"$(message)\"}}";
// Register for notifications.
// (gcmRegId, ["tag1","tag2"], templateName, templateBody)
hub.gcm.register(e.regid, null, "myTemplate", template).done(function () {
alert("Registered with hub!");
}).fail(function (error) {
alert("Failed registering with hub: " + error);
});
}
}
break;
case 'message':
console.log("received message: " + e.message);
if (e.foreground) {
alert(e.payload.message);
}
break;
case 'error':
alert('GCM error: ' + e.message);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
【问题讨论】:
-
我遇到了同样的结果。有结果吗?
标签: cordova phonegap-plugins azure-mobile-services