我正在尝试将通知从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字符串有效负载= eyJhcHMiOnsiYWxlcnQiOiJ7 ...看起来像这样
{
"aps": {
"alert": "{\"aps\":{\"alert\":\"You got your emails.\",\"badge\":9,\"sound\":\"bingbong.aiff\"},\"acme1\":\"bar\",\"acme2\":42}",
"sound": "default"
}
}
和ios手机会收到这个难看的字符串“ {\” aps \“:{\”警报\“:\”您收到了电子邮件。\“,\”徽章\“:9,\”声音\“ :\“ bingbong.aiff \”},\“ acme1 \”:\“ bar \”,\“ acme2 \”:42}“作为通知。如何正确发送到ios js对象?我尝试在对象中使用不同的键,但始终在ios mobile上得到字符串化的响应。
要实现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))
};