Javascript Quickblox字符串化通知
我正在尝试从quickblox js sdk将通知推送到ios。 我有一些示例代码。QuickBlox JavaScript SDK:2.12.7Javascript Quickblox字符串化通知,javascript,node.js,quickblox,Javascript,Node.js,Quickblox,我正在尝试从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 Qui
'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字符串有效负载=eyJhcHMiOnsiYWxlcnQiOiJ7。。。看起来像这样
{
"aps": {
"alert": "{\"aps\":{\"alert\":\"You got your emails.\",\"badge\":9,\"sound\":\"bingbong.aiff\"},\"acme1\":\"bar\",\"acme2\":42}",
"sound": "default"
}
}
ios手机会收到这样一个丑陋的字符串:{“aps\”:{“alert\”:“You's your email.\”,“badge\”:9,“sound\”:“bingbong.aiff\”,“acme1\”:“bar\”,“acme2\”:42}作为通知。
如何正确发送到ios js对象?
我在一个对象中尝试了不同的键,但在ios mobile上总是得到一个字符串化的响应。要实现仅ios推送通知,base64编码的字符串应该在前面加上“payload=”如中所示 尝试使用以下方法:
event[message]=payload=ew0KICAgICJhcHMiIDogew0KICAgICAgICAiYWxlcnQiIDogIllvdSBnb3QgeW91ciBlbWFpbHMuIiwNCiAgICAgICAgImJhZGdlIiA6IDksDQogICAgICAgICJzb3VuZCIgOiAiYmluZ2JvbmcuYWlmZiINCiAgICB9LA0KICAgICJhY21lMSIgOiAiYmFyIiwNCiAgICAiYWNtZTIiIDogNDINCn0=
顺便说一句,使用您可以实现相同的行为,但推送通知将发送到更多的平台
要发送通用推送通知,您应该做一个小更改:
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))
};