uni-app中的推送

需求:最近公司要做推送,用的是uni-app,这里备注一下

 

App.vue里这样操作:

分别是iOS 和Android的在线创建推送,以及点击事件的处理,这里点击事件存储一下,然后发送消息在首页处理推送。

如果在这里处理,会有先跳转推送页再返回首页的问题。

    plus.push.addEventListener('click', function (msg) {
      // 存储一下点击后的推送信息
      // uni.setStorageSync('receive', msg);
      // 2 处理跳转  如果没登录 去登录
      // const resos = uni.getSystemInfoSync();
      // if (resos.platform === 'ios') {}
      if (!userStore.isLogin) {
        uni.switchTab({
          url: '/pages/autoLogin/index',
        });
        return;
      }
      if (msg.aps) {
        // 离线 到首页再执行
        uni.setStorageSync('msg_payload', msg.payload);
        uni.$emit('msgPayload');
      } else {
        // 在线可立即执行 但是不行,离线状态点击在线推送不行,所以也到首页再执行
        uni.setStorageSync('msg_payload', msg.payload);
        uni.$emit('msgPayload');
      }
    });
    // 在线推送点击  receive 内创建推送弹窗
    uni.onPushMessage(res => {
      if (res.type == 'click') {
        console.log('点击业务在addEventListener处理');
      }
      if (res.type == 'receive') {
        // 创建推送显示
        // uni.setStorageSync('receive', res);
        // 创建推送显示  ios
        console.log('===onPushMessage======res=============', res);
        const resos = uni.getSystemInfoSync();
        if (resos.platform === 'ios') {
          var content_obj = JSON.parse(res.data.content);
          let titleStr = decodeURIComponent(content_obj.title);
          let contentStr = decodeURIComponent(content_obj.content);
          uni.createPushMessage({
            icon: '',
            title: titleStr || '连xx',
            content: contentStr || '您有一个通知',
            payload: content_obj.url,
            success: res => {
              console.log('成功创建', res);
            },
          });
        } else {
          // 创建推送显示  android
          let contentStr = decodeURIComponent(res.data.content);
          uni.createPushMessage({
            icon: '',
            title: res.data.title || '连xx',
            content: contentStr || '您有一个通知',
            payload: res.data.payload.url,
            success: res => {
              console.log('成功创建', res);
            },
          });
        }
      }
      plus.runtime.setBadgeNumber(0);
    });
    // 获取客户端标识
    uni.getPushClientId({
      success: res => {
        let push_clientid = res.cid;
        uni.setStorageSync('cid', push_clientid);
      },
    });
    // #ifdef APP-PLUS
    let pinf = plus.push.getClientInfo();
    if (pinf && pinf.clientid) {
      uni.setStorageSync('cid', pinf.clientid);
    } else {
      var obtainingCIDTimer = setInterval(function () {
        pinf = plus.push.getClientInfo();
        if (pinf.clientid) {
          uni.setStorageSync('cid', pinf.clientid);
          clearInterval(obtainingCIDTimer);
        }
      }, 50);
    }
    // #endif
    plus.runtime.setBadgeNumber(0);

首页这样处理:

如果是离线推送,冷启动,外部的msgPayload()生效,在线推送的话,

uni.$on('msgPayload', () => {
    msgPayload();
  });
生效。跳转后,移除存储的跳转信息。
onReady(() => {
  msgPayload();
  uni.$on('msgPayload', () => {
    msgPayload();
  });
});

// 不同意隐私 关闭Model
function msgPayload() {
  let payload = uni.getStorageSync('msg_payload');
  if (payload && userStore.isLogin) {
    var redirectUrl = decodeURIComponent(payload);
    console.log('=离线=======redirectUrl=======', redirectUrl); // 123456
    setTimeout(() => {
      if (isHttpLink(redirectUrl)) {
        redirectUrl = getJoinSSOUrl(redirectUrl, userStore.token);
      }
      if (isTabBarLink(redirectUrl)) {
        uni.switchTab({
          url: redirectUrl,
        });
      } else {
        jumpSupport(redirectUrl, '1');
      }
    }, 500);
    setTimeout(() => {
      uni.removeStorageSync('msg_payload');
    }, 1000);
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章