uniapp 使用 uni push 2.0 推送消息

因为之前使用uni push 1.0,开通账号和配置厂商就不写了。只说一点,配置厂商很重要,不然收不到离线推送的消息。那么就直接开始咯!!!

一、创建并关联云服务空间

1.创建云服务空间,右键项目【创建uniClound云开发环境】,然后选择运营商【腾讯云、阿里云】。

2.关联云服务空间,如图:右键【uniCloud】===>点击【关联云服务空间或项目】然后选择需要关联的云服务空间名称,点击关联。

  3.新建云函数,如图所示

4.在云函数中添加以下代码

'use strict';
const uniPush = uniCloud.getPushManager({appId:"__UNI__XXXXXX"})
exports.main = async (event) => {
const res = await uniPush.sendMessage({
    "push_clientid": event.cids,
    "title": event.title,
    "content": event.content,
    "payload": event.data,
    "force_notification": true,
    "request_id": event.request_id
})
return res;
};

二、调用云函数推送信息

function getId() {
    let yy = new Date().getFullYear();
    let mm = new Date().getMonth() + 1;
    let dd = new Date().getDate();
    let hh = new Date().getHours();
    let mf = new Date().getMinutes() < 10 ? "0" + new Date().getMinutes() : new Date().getMinutes();
    let ss = new Date().getSeconds() < 10 ? "0" + new Date().getSeconds() : new Date().getSeconds();
    let Randnum = "";
    for (var i = 0; i < 10; i++) {
        Randnum += Math.floor(Math.random() * 10);
    }
    return "ID_" + yy + mm + dd + hh + mf + ss + Randnum;
}

function appPushInfo(cids, title, content, data) {
return new Promise((resolve) => {
    const request_id = getId();
    uniCloud.callFunction({
    name: '云函数名称',
    data: {
    cids,
    title,
    content,
    data,
    request_id
    }
}).then((res) => {
    resolve(res.success)
}).catch(() => {
    resolve(false);
})
});
}

#个人对推送信息的方法进行了封装,相信配置请看官网api uni-app官网 (dcloud.net.cn)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值