gpt4 book ai didi

javascript - Quickblox 字符串化通知

转载 作者:行者123 更新时间:2023-12-03 16:40:43 27 4
gpt4 key购买 nike

我正在尝试将通知从 quickblox js sdk 推送到 ios。我有一些示例代码。 QuickBlox JavaScript SDK:2.12.7

'use strict';

const QuickBlox = require('quickblox').QuickBlox;

const CREDENTIALS = {
appId: 'appId',
authKey: 'authKey',
authSecret: 'authSecret'
};
const QB = new QuickBlox();

QB.init(CREDENTIALS.appId, CREDENTIALS.authKey, CREDENTIALS.authSecret);

function createSession() {
return new Promise((resolve, reject) => {
QB.createSession(function(err, result) {
if (err) {
console.log(err);
reject(err);
}
resolve(result);
});
});
}

function pushNotification(userIds, message) {
const params = {
notification_type: 'push',
push_type: 'apns',
user: {ids: userIds},
environment: 'development',
message: QB.pushnotifications.base64Encode(message)
};

return new Promise((resolve, reject) => {
QB.pushnotifications.events.create(params, function(err, response) {
if (err) {
console.log(err);
reject(err);
}
resolve(response);
});
});
}

function loginUser(email, password) {
const params = { email, password };

return new Promise((resolve, reject) => {
QB.login(params, function(err, user){
if (user) {
resolve(user);
} else {
reject(err);
}
});
});
}


const iosMessage = {
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
"acme1" : "bar",
"acme2" : 42
};

(async () => {
try {
await createSession();
await loginUser('email', 'password');
const send = await pushNotification(['userId'], JSON.stringify(iosMessage));
console.log(send);
} catch (err) {
console.log(err);
}
})();

它成功发送消息我收到了这个回复。

{
"event": {
"id": 31147883,
"event_type": "one_shot",
"message": "payload=eyJhcHMiOnsiYWxlcnQiOiJ7XCJhcHNcIjp7XCJhbGVydFwiOlwiWW91IGdvdCB5b3VyIGVtYWlscy5cIixcImJhZGdlXCI6OSxcInNvdW5kXCI6XCJiaW5nYm9uZy5haWZmXCJ9LFwiYWNtZTFcIjpcImJhclwiLFwiYWNtZTJcIjo0Mn0iLCJzb3VuZCI6ImRlZmF1bHQifX0=",
"date": null,
"period": null,
"name": null,
"occured_count": 0,
"created_at": "2019-12-16T20:50:14Z",
"updated_at": "2019-12-16T20:50:14Z",
"end_date": null,
"active": true,
"application_id": 63650,
"user_id": 35292731,
"kind": "API",
"environment": "development",
"tag_query": null,
"notification_channel": {
"name": "apns"
}
}
}

问题是这个 base64 字符串 payload=eyJhcHMiOnsiYWxlcnQiOiJ7... 看起来像这样

{
"aps": {
"alert": "{\"aps\":{\"alert\":\"You got your emails.\",\"badge\":9,\"sound\":\"bingbong.aiff\"},\"acme1\":\"bar\",\"acme2\":42}",
"sound": "default"
}
}

而 ios 手机得到这个丑陋的字符串 "{\"aps\":{\"alert\":\"You got your emails.\",\"badge\":9,\"sound\":\"bingbong.aiff\"},\"acme1\":\"bar\",\"acme2\":42}" 作为通知。如何正确发送到 ios js 对象?我在一个对象中尝试了不同的键,但在 iOS 移动设备上总是得到一个字符串化的响应。

最佳答案

要实现仅 iOS 推送通知,base64 编码字符串应在前面加上“payload=”,如 QuickBlox REST API Platform based push notification section 中所示。 .

尝试使用这个:

event[message]=payload=ew0KICAgICJhcHMiIDogew0KICAgICAgICAiYWxlcnQiIDogIllvdSBnb3QgeW91ciBlbWFpbHMuIiwNCiAgICAgICAgImJhZGdlIiA6IDksDQogICAgICAgICJzb3VuZCIgOiAiYmluZ2JvbmcuYWlmZiINCiAgICB9LA0KICAgICJhY21lMSIgOiAiYmFyIiwNCiAgICAiYWNtZTIiIDogNDINCn0=

顺便说一句,使用 Universal Push Notifications您可以实现相同的行为,但推送通知将传送到更多平台。

要发送通用推送通知,您应该做一个小改动:

const iosMessage = {
message: "You got an email.",
ios_badge: 9,
ios_sound: "bingbong.aiff"
acme1: "bar",
acme2: 42
};
const params = {
notification_type: 'push',
user: {ids: userIds},
environment: 'development',
message: QB.pushnotifications.base64Encode(JSON.stringify(iosMessage))
};

关于javascript - Quickblox 字符串化通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59369632/

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