信鸽推送 push API
参考: push_api_v3
0. 基本 push
const _ = require('lodash');
const fetch = require('node-fetch');
const CryptoJS = require('crypto-js');
// zh/en android/ios authorization
const XINGE_PARAMS = {
zh: {
android: {
appId: '', // 需要替换成自己app的appId
secretKey: '', // 需要替换成自己app的secretKey
},
ios: {
appId: '',
secretKey: '',
},
},
en: {
android: {
appId: '',
secretKey: '',
},
ios: {
appId: '',
secretKey: '',
},
},
};
const getAuthorization = (locale, platform) => {
const { appId, secretKey } = XINGE_PARAMS[locale][platform];
const str = `${appId}:${secretKey}`;
const wordArray = CryptoJS.enc.Utf8.parse(str);
const base64AuthString = CryptoJS.enc.Base64.stringify(wordArray);
return base64AuthString;
};
const XINGE_AUTHORIZATIONS = ['zh', 'en'].reduce((authorizations, locale) => {
const platforms = ['android', 'ios'].reduce((platformResult, platform) => {
platformResult[platform] = getAuthorization(locale, platform);
return platformResult;
}, {});
authorizations[locale] = platforms;
return authorizations;
}, {});
// android/ios notification message;
const formatMessageByPlatform = (platform, custom = {}) => {
if (platform === 'android') {
return {
android: {
action: {
action_type: 1,
},
custom_content: custom, //用户自定义的键值对
},
};
}
return {
ios: {
aps: {
alert: {},
sound: 'default',
},
custom,
}
};
};
// common push, audience_type default value is all
const push = (params, audienceOptions) => {
const { locale, platform, title, content, custom, environment = 'product' } = params;
const authorization = XINGE_AUTHORIZATIONS[locale][platform];
const otherMessage = formatMessageByPlatform(platform, custom);
const environmentObj = platform === 'ios' ? { environment } : {};
const data = Object.assign({
audience_type: 'all',
platform,
...environmentObj,
message_type: 'notify',
message: {
title,
content,
...otherMessage,
},
},
audienceOptions);
return fetch('https://openapi.xg.qq.com/v3/push/app', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${authorization}`,
},
body: JSON.stringify(data),
})
.then((res) => res.json())
.then((res) => console.log(res));
};
exports.push = push;
1. 根据 token list,推送到android和ios
function getTokensByPlatform(platform, tokens) {
return tokens
.filter((item) => item.platform === platform)
.map((item) => item.token);
}
// 1. push to tokenList by tokens
exports.xgPushByTokens= (params, tokens) => {
const androidParams = { platform: 'android', ...params };
const androidTokens = getTokensByPlatform('android', tokens);
const androidAudience = {
audience_type: 'token_list',
token_list: androidTokens,
};
const iosParams = { platform: 'ios', ...params };
const iosTokens = getTokensByPlatform('ios', tokens);
const iosAudience = {
audience_type: 'token_list',
token_list: iosTokens,
};
return Promise.all([
_.isEmpty(androidTokens) ? Promise.resolve() : push(androidParams, androidAudience),
_.isEmpty(iosTokens) ? Promise.resolve() : push(iosParams, iosAudience),
]);
};
2. 推送到android和ios 所有用户
// 2. push to all
exports.xgPushAll = (params) => {
const androidParams = { platform: 'android', ...params };
const iosParams = { platform: 'ios', ...params };
return Promise.all([
push(androidParams),
push(iosParams),
]);
};