uniapp对接unipush 1.0 ios/android

简介

实现方法 是uniapp官网推荐的 unipush-v1

文档配置具体看 uni-app官网

配置好了之后

代码实现

前端代码

前端的主要任务是监听 监听到title content 创建消息推送

安卓 可以收到在线消息并且自动弹出消息

IOS 可以监听到在线消息但是需要手动推送

以下代码app初始换完成就可运行

javascript 复制代码
onPushMessage() {
         uni.onPushMessage(res => { 
            let platform = uni.getSystemInfoSync().platform;
            let {
                type,
                data: {
                    payload = {}
                }
            } = res || {};
            let {
                title = '',
                    content = '',
                    path = ''
            } = payload; 
            if (path) {
                if (platform == 'ios' && type == "receive") {
                    uni.createPushMessage({
                        title,
                        content,
                        payload,
                        success() {
                            console.log("推送成功");
                        },
                        fail() {
                            console.log("推送失败");
                        }
                    })
                    return
                }
                uni.navigateTo({
                    url: path,
                    fail() {
                        uni.showToast({
                            title: "跳转失败"
                        })
                    }
                })
            } 
        })
    },

node后端参考代码

javascript 复制代码
const axios = require("axios");
const crypto = require("crypto");
const appData = {
  appID: "",
  appKey: "",
  appSecret: "",
  masterSecret: "",
};
const baseUrl = "https://restapi.getui.com/v2/eX4SOsJhiqAwaOuODvyDr";
let toAppUrl = `${baseUrl}/push/all`;
let tokenUrl = `${baseUrl}/auth`;
let singleUrl = `${baseUrl}/push/single/cid`;

// let token = "";
function getTime() {
  return new Date().getTime();
}
function hash(text) {
  const hash = crypto.createHash("sha256");
  hash.update(text);
  return hash.digest("hex");
}
function getSign(appkey, timestamp, mastersecret) {
  return hash(appkey + timestamp + mastersecret);
}
function getToken(url = tokenUrl) {
  let { appID, appKey, appSecret, masterSecret } = appData;
  let timestamp = getTime();
  return new Promise((resolve, reject) => {
    axios({
      method: "post",
      url,
      data: {
        sign: getSign(appKey, timestamp, masterSecret),
        timestamp,
        appkey: appKey,
      },
    }).then((res) => {
      if (res.data.code == 0) {
        resolve(res.data.data.token);
      } else {
        reject(res);
      }
    });
  });
}
function schedule(taskid, token) {
  axios({
    method: "GET",
    url: `${baseUrl}/task/schedule/${taskid}`,
    headers: {
      token: token,
    },
  }).then((res) => {
    console.log("返回结果", res.data);
  });
}
async function single(url) {
  let request_id = getTime();
  let channel_id = getTime();

  let token = await getToken(tokenUrl);
  axios({
    method: "post",
    url,
    headers: {
      token: token,
    },
    data: {
      request_id,
      settings: {
        ttl: 7200000,
      },
      audience: { 
        // 荣耀 5025894ea89b90b277affc77e3964029
        // 7327ae82e78a1840d367c4cf1411daeb 华为cid
        // 我的苹果 dd49a3f62e68d846ba9d8ab88a8a738b
        cid: [cid]
      },
      push_message: {  
        notification: {
          title: "提示",
          body: "通知内容",
          big_text: "通知内容",
          channel_id: "Default",
          channel_name: "Default",
          channel_level: 4,
          // click_type: "payload", 
         // click_type: "intent",
          //intent:"intent://io.dcloud.unipush/?#Intent;scheme=unipush;launchFlags=0x4000000;component=包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=;S.content=测试内容;S.payload={title: '自定义消息内容',path:'/Seting/index/index'};end",
            click_type: "payload", 
          payload: JSON.stringify({
            title: "自定义消息内容在线通知",
            content:"自定义消息内容在线通知内容", 
            path:"/Seting/index/index"
          })
        }
      },
      push_channel: {
        android: {
          ups: {
            notification: {
              title: "离线通知标题",
              body: "厂商通知内容",
              click_type: "intent",
              intent:`intent://io.dcloud.unipush/?#Intent;scheme=unipush;launchFlags=0x4000000;component=app包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=;S.content=测试内容;S.payload={title: '自定义消息内容',path:'/Seting/index/index'};end`,
            },
            options: {
              HW: {
                "/message/android/notification/badge/class":"io.dcloud.PandoraEntry",
                "/message/android/notification/badge/add_num": 1,
                "/message/android/category": "EXPRESS",

              },
            },
          },
        },
        ios: {
          type: "notify",
          payload: JSON.stringify({
            title: "自定义消息内容离线通知",
            path:"/Seting/index/index" 
          }),
          aps: {
            alert: {
              title: "通知标题",
              body: "通知内容"
            },
            "content-available": 0,
            sound: "default",
            category: "ACTIONABLE",
          },
          auto_badge: "+1",
        },
      },
    },
  }).then((res) => {
    console.log(res.data);
  });
}
// toApp(toAppUrl);
single(singleUrl);
相关推荐
CCSBRIDGE2 小时前
Magento2项目部署笔记
笔记
亦枫Leonlew3 小时前
微积分复习笔记 Calculus Volume 2 - 5.1 Sequences
笔记·数学·微积分
爱码小白3 小时前
网络编程(王铭东老师)笔记
服务器·网络·笔记
LuH11244 小时前
【论文阅读笔记】Learning to sample
论文阅读·笔记·图形渲染·点云
一棵开花的树,枝芽无限靠近你5 小时前
【PPTist】组件结构设计、主题切换
前端·笔记·学习·编辑器
犬余6 小时前
设计模式之桥接模式:抽象与实现之间的分离艺术
笔记·学习·设计模式·桥接模式
数据爬坡ing7 小时前
小白考研历程:跌跌撞撞,起起伏伏,五个月备战历程!!!
大数据·笔记·考研·数据分析
咖肥猫7 小时前
【ue5学习笔记2】在场景放入一个物体的蓝图输入事件无效?
笔记·学习·ue5
郭尘帅6668 小时前
Ajax学习笔记
笔记·学习·ajax
我叫啥都行9 小时前
计算机基础复习12.23
java·开发语言·笔记·后端·学习