gpt4 book ai didi

javascript - 向 Android 设备发送通知。已发送但未收到

转载 作者:搜寻专家 更新时间:2023-11-01 08:40:02 24 4
gpt4 key购买 nike

我越来越熟悉向设备发送通知,

我目前正在尝试使用 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% 免费的,并且还使用了谷歌的云消息服务。基本上在安装他们的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com