我正在熟悉如何向设备发送通知,
我目前正在尝试使用 pubnub 和 Google 云消息传递向我的设备发送通知:
(function() {
var pubnub = PUBNUB.init({
subscribe_key: 'my-subscribe',
publish_key: 'my-publish',
});
var os = 'android';
var regid = '';
var channel = 'mi-canal';
function sendPush() {
var gwtype = (os === 'ios') ? 'apns' : 'gcm';
console.log('sending push notification...',channel,gwtype);
pubnub.mobile_gw_provision({
device_id: 'APA91bE0rX8mfzY0VttACffUPIagSbd5No0xxxxxxxxxxxxxxxxxEHCVFBbzPqPHKG9phyQ31O4W6aEgMUqWANtWrK-zdMsiONUwAbUW4efso8ScemlXGM8tJq0M12oR0E2Kk9',
channel: channel,
op: 'add',
gw_type: gwtype,
error: function(msg){console.log(msg);},
callback: function() {
var message = PNmessage();
message.pubnub = pubnub;
message.callback = function (msg){ console.log(msg); };
message.error = function (msg){ console.log(msg); };
message.channel = channel;
message.apns = {
alert: 'The room temperature is set too high'
};
message.gcm = {
title: 'PubNub Push Demo',
message: 'The room temperature is set too high'
};
message.publish();
}
});
}
sendPush();
})();
哪些日志:
[1, "已发送", "14471821928645156"]
问题是我在设备中没有收到 Android 应用程序(cordova 制作)的通知
var pushNotification = window.plugins.pushNotification;
//pushNotification.unregister(successHandler, errorHandler);
pushNotification.register(
successHandler,
errorHandler,
{
'senderID':'api-project-xxxxxxxx',
'ecb':'onNotificationGCM' // callback function
}
);
pusNotification.subscribe({
channel: 'mi-canal',
message: function(m){
alert('Conslog: '+m);
},
error: function (error) {
// Handle error here
console.log(JSON.stringify(error));
}
});
知道我错过了什么吗?
最佳答案
我个人使用OneSignal在我的 Android 应用程序上,它就像一个魅力。 您可以阅读文档here 。它是 100% 免费的,并且还使用 Google 的云消息服务。基本上在安装了他们的 SDK 并遵循整个文档之后,您的代码将是这样的。
代码示例:
document.addEventListener('deviceready', function () {
// Enable to debug issues.
// window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});
var notificationOpenedCallback = function(jsonData) {
console.log('didReceiveRemoteNotificationCallBack: ' + JSON.stringify(jsonData));
};
window.plugins.OneSignal.init("b2f7f966-d8cc-11e4-bed1-df8f05be55ba",
{googleProjectNumber: "703322744261"},
notificationOpenedCallback);
// Show an alert box if a notification comes in when the user is in your app.
window.plugins.OneSignal.enableInAppAlertNotification(true);
}, false);
- Replace "b2f7f966-d8cc-11e4-bed1-df8f05be55ba" with your OneSignal App Id.
- Replace "703322744261" with your Google Project Number.
对我来说,当我发送通知后,我的手机会在 3 秒内发出蜂鸣声。希望这有帮助:)
关于javascript - 向 Android 设备发送通知。已发送但未收到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33637685/