uniapp对接unipush 1.0 ios/android

发布于:2024-12-21 ⋅ 阅读:(36) ⋅ 点赞:(0)

简介

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

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

配置好了之后
代码实现

前端代码

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

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

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

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

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后端参考代码

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);


网站公告

今日签到

点亮在社区的每一天
去签到