Uniapp provides unipush service, but every time you want to push a message, you have to log in the developer background of Dcloud, which is a little inconvenient. The operation can be completed in our background system.

How to use Dcloud’s uniPush

The official description of the basic use of UNIpush is very detailed, but it needs to be connected to offline push, and it needs to apply to various manufacturers, or it can only be pushed online

Push a butt

I’m not going to take offline messages into account, but the online message tests are basically successful, because a single push requires a CID, and I need to change the code to get the CID, so I’m going to take group push as an example and I’m connecting to RestAPI V2 which is a push

  • The app end
// This code is used to retrieve the CID from the device. This code is used to retrieve the CID from the device every time the user enters the device. Save to database const pinf = plus.push.getClientInfo() const cid= pinf.clientid console.log(' cid=${cid} ') // Listen for push notifications plus.push.addEventListener('receive', MSG = > {this. GlobalData. Test1 = JSON. Stringify (MSG)}) / / monitor. Click on the notification bar plus push. AddEventListener (' click ', msg => { this.globalData.test2 = JSON.stringify(msg) })Copy the code
  • Server: Obtains the token
async getGeTuiToken() {
    const { ctx } = this
    const { appId, appKey, appSecret, masterSecret } = accounts.appPush
    const timestamp = Date.now()
    const sign = crypto.createHash('sha256').update(`${appKey}${timestamp}${masterSecret}`).digest('hex')
    const res = await ctx.curl(`${baseUrl}${appId}/auth`, {
        method: 'POST'.contentType: 'json'.data: {
            sign,
            timestamp,
            appkey: appKey
        },
        dataType: 'json'
    });
    return res.data.data
}
Copy the code
  • Server: Group push
async toApp() { const { ctx, app, service } = this const { appId } = accounts.appPush let token = await service.cache.get('getui_token') if (! token) { token = await service.push.getGeTuiToken() } const { title, body_msg, big_text, click_type, Payload} = ctx.request.body console.log(title, body_msg, big_text, click_type, payload) Because click_type determines payload if (! title || ! body_msg || ! Click_type) return ctx.body = 'error' const result = await ctx.curl(' ${baseUrl}${appId}/push/all ', {method: 'POST', contentType: 'json', dataType: 'json', data: { request_id: 'june' + Date.now() + 'push', audience: 'all', push_message: { notification: { title, body: body_msg, big_text, channel_level: 3, click_type: 'payload', payload } }, push_channel: { android: { ups: { notification: { title, body: body_msg, click_type: 'payload', payload } } } } }, headers: { token } }); ctx.body = result }Copy the code

END

This is my test code, push group only need to pay attention to the controller/common/push toApp the warehouse for testing with js, oss, cos, WeChat pay, share the signature, need to fill out some accounts under the config directory

Module. Exports = {token: ", token: ", baseUrl: "}, wechatMini: {// token: ", module. Exports = {token: ", baseUrl: "}, wechatMini: {// token: ", token: ", baseUrl: "} ", appSecret: "}, wetchatApp: {// appId: ", appSecret: "}, wechatPay: {// MCHID: ", key: AppSecret: ", appSecret: ", masterSecret: "}, tx: {// Tencent Cloud appId:" ", secretId: ", secretKey: "}, tx_cos: {// Tencent cos bucket: ", region: "}, ali_oss: {accessKeyId: '', accessKeySecret: '', bucket: '', endpoint: '' } }Copy the code