Uni-app 之uni-push1.0服务端推送

一、配置

1、uni-push1.0文档

https://uniapp.dcloud.net.cn/unipush-v1.html

2、服务端推送文档

https://docs.getui.com/getui/server/rest_v2/push/

二、客户端

1、App.vue

import phoneInfo from '@/common/js/phone-info.js';
import uniPushListener from '@/common/js/unipush.js';
import updateApp from '@/common/js/update.js';

 

// #ifdef APP-PLUS
uniPushListener.getInfo();
plus.push.setAutoNotification(true);  //设置通知栏显示通知 //必须设置
plus.screen.lockOrientation('portrait-primary'); //锁定屏幕方向
uni.setStorageSync('cancelUpdate', 'false'); // 进来APP 重置更新弹窗
// 获取App 当前版本号
if (Object.keys(uni.getStorageSync('widgetInfo')).length == 0) {
    plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
        phoneInfo.manifestInfo = widgetInfo;
        uni.setStorageSync('widgetInfo', widgetInfo);
    });
}
uniPushListener.getClientInfoLoop(); // 循环获取cid

uniPushListener.pushListener(); // 监听通知栏信息

plus.runtime.setBadgeNumber(0); //清除app角标
plus.runtime.setBadgeNumber(-1);

//检查版本更新
var platform = phoneInfo.systemInfo.platform;
var version = phoneInfo.systemInfo.appWgtVersion.toLowerCase();
var _this = this;
updateApp.checkUpdate(version, platform, function(res){
  _this.$showModal({
      title: '提示',  
      content: res.data.msg,
      confirmText: '立即更新',
    showCancel: false
  }).then(ret => {
    plus.runtime.openURL(res.data.path);
  })
})
//#endif

2、phone-info.js

export default {
    systemInfo: {}, // 系统设备信息
    manifestInfo: "" || uni.getStorageSync("widgetInfo"), // manifest.json 应用信息
}

3、unipush.js

// 监听push消息 以及 后台数据回复
import phoneInfo from '@/common/js/phone-info.js';
let timer = null;
let numloop = 0;
import {
    doRequest
} from "@/common/js/request.js"

// 消息推送 应用配置(这些给后端用的)
const uniPushObj = {
    cid: "",
    AppID: "",
    AppKey: "",
    AppSecret: "",
    MasterSecret: "",
}

export default {
    getInfo() {
        uni.getSystemInfo({
            success: res => {
                phoneInfo.systemInfo = res;
            }
        });
    },
    // 开启监听推送 
    pushListener() {
        const platform = phoneInfo.systemInfo.platform.toLowerCase();
        // 点击推送信息
        plus.push.addEventListener('click', res => {
            const token = uni.getStorageSync("userid");
            console.log("(click):" + JSON.stringify(res));
            
            // 其实在这里就可以根据你自己的业务去写了
            plus.push.clear(); //清空通知栏
            if (token) {
                messageClick(res);
            } else {
                // 这里跳登录页了
                uni.navigateTo({
                    url: `/pages/login/index`
                })
            }
        });
        // 接收推送信息  在线
        plus.push.addEventListener('receive', res => {
            console.log("(receive):" + JSON.stringify(res));

            const messageTitle = res.title;
            const messageContent = res.content;
            if (platform == 'android') {
                /***  
                  安卓监听不到  因为安卓这个格式被封装了,做成了通知栏展示
                  换个格式就行(比如里面多个字段,或换个字段名)
                */
                /***
                  此格式的透传消息由 unipush 做了特殊处理, 会自动展示通知栏 
                  开发者也可自定义其它格式, 在客户端自己处理
                */
                //   "push_message": {
                //     "transmission": "{
                //       title:\"标题\",
                //       content:\"内容\",
                //       payload:\"自定义数据\"
                //     }"
                //   },
                // Hbulidx 版本大于 ## 3.4.18,安卓不再通知栏展示, 需要自行创建通知
                plus.push.createMessage(messageContent, res.payload, {
                    title: messageTitle
                });
                // 或者在 onlaunch 写入
                // plus.push.setAutoNotification(true);
            } else {
                const type = res.type
                //【APP离线】收到消息,但没有提醒(发生在一次收到多个离线消息时,只有一个有提醒,但其他的没有提醒)  
                //【APP在线】收到消息,不会触发系统消息,需要创建本地消息,但不能重复创建
                // 必须加msg.type验证去除死循环        
                if (res.aps == null && type == "receive") {
                    //创建本地消息,发送的本地消息也会被receive方法接收到,但没有type属性,且aps是null  
                    plus.push.createMessage(messageContent, res.payload, {
                        title: messageTitle
                    });
                }
            }
        });

        function messageClick(msg) {
            if (typeof(msg.payload) == 'string') { //如果是字符串,表示是ios创建的  要转换一下
                msg.payload = JSON.parse(msg.payload)
            }
            if (!msg) return false;
            try {
                var page = msg.payload.page;
                switch (msg.payload.type) {
                    case 'switchTab':
                        uni.switchTab({
                            url: page
                        })
                        break;
                    case 'navigateTo':
                        uni.navigateTo({
                            url: page
                        })
                        break;
                    case 'redirectTo':
                        uni.redirectTo({
                            url: page
                        })
                        break;
                }
            } catch (e) {
                console.log(e)
            }
        }
    },
    // 循环获取clientid信息,直到获取到为止
    getClientInfoLoop() {
        plus.push.getClientInfoAsync(info => {
            // 如果info不存在,或者info存在,cid不存在则再次获取cid
            if (!info || !info.clientid) {
                console.log("cid为空=========================================");
                let infoTimer = null;
                infoTimer = setInterval(function() {
                    if (cid) {
                        clearInterval(infoTimer); //清定时器
                        uni.showModal({
                            content: cid
                        })
                        uni.setStorageSync('cid', cid);
                        uniPushObj.cid = cid
                    }
                }, 50);
            } else if (info && info.clientid) {
                let cid = info.clientid;
                uni.setStorageSync('cid', cid);
                uniPushObj.cid = cid
            }
        }, function(e) {
            console.log('Failed', JSON.stringify(e));
            let pinf = plus.push.getClientInfo();
            let cid = pinf.clientid; //客户端标识 
            if (cid) {
                uni.setStorageSync('cid', cid);
                uniPushObj.cid = cid
            }
        })
    },
    /** 
     * 向后台传送cid,绑定别名
     */
    passCid() {
        var params = {
            action: 'app_bind_cid',
            appid: uniPushObj.AppID,
            cid: uniPushObj.cid,
            userid: uni.getStorageSync('userid')
        };
        console.log(params)
        doRequest(params).then(response => {
            console.log('----------> cid 绑定别名成功', response);
        })
    },
}

4、update.js

import {
    doRequest
} from "@/common/js/request.js"

export default {
    //获取已登陆用户信息
    checkUpdate(version, platform, callback=function(){}) {
        doRequest({
            action: 'app_version',
            version: version,
            platform: platform
        }).then(res => {
            if (res.code == 0) {
                callback(res)
            }
        })
    }
}

 

三、服务端示例

//0:站内信,1:消息
$type = isset($type) ? $type : 0;
$clickType = 'intent';
$payload = array('type' => 'switchTab', 'page' => $type == 1 ? '/pages/chat/index' : '/pages/message/index');
$url = "";
$data = array(
    'cid' => $cid,
    'title' => $title,
    'content' => $content,
    'clickType' => $clickType,
    'payload_data' => json_encode($payload),
    'url' => $url,
    'intent_data' => "intent://io.dcloud.unipush/?#Intent;scheme=unipush;launchFlags=0x4000000;component=com.boyuan.ycd.app/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=".urlencode($title).";S.content=".urlencode($content).";S.payload=".json_encode($payload).";end"
);

write_log(json_encode($data, JSON_UNESCAPED_UNICODE));

pushToSingleByCid($data);

/**
 * 发送透传消息
 * @param $data
 * @return bool
 */
function pushToSingleByCid($data){
    //创建API,APPID等配置参考 环境要求 进行获取
    $api = new GTClient(URL,APPKEY, APPID,MS);
    $setting = new GTSettings();
    $setting->setTtl(3600000);

    //设置推送参数
    $push = new GTPushRequest();
    $push->setRequestId(time());
    $message = new GTPushMessage();
    $notify = new GTNotification();
    $pushChannel = new GTPushChannel();

    //配置推送条件
    $push->setSettings($setting);

    //设置个推
    $notify->setTitle($data['title']);
    $notify->setBody($data['content']);
    $notify->setChannelLevel(3);
    //点击通知后续动作,目前支持以下后续动作:
    //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
    $notify->setClickType($data['clickType']);
    if ($data['clickType'] == 'payload') {
        $notify->setPayload($data['payload_data']);
    }
    if ($data['clickType'] == 'url') {
        $notify->setUrl($data['url']);
    }
    if ($data['clickType'] == 'intent') {
        $notify->setIntent($data['intent_data']);
    }
    $message->setNotification($notify);
    $push->setPushMessage($message);

    //设置安卓离线厂商通道推送消息体
    $androidDTO = new GTAndroid();
    $ups = new GTUps();
    $ups->addOption("VV", "/category", "SUBSCRIPTION");
    $ups->addOption("XM", "/extra.channel_id", "通道id");
    $ups->addOption("OP", "/channel_id", "通道id");
    $ups->addOption("HW", "/message/android/category", "CATEGORY_REMINDER");
    $ups->addOption("HW", "/message/android/notification/badge/class", "io.dcloud.PandoraEntry");
    $ups->addOption("HW", "/message/android/notification/badge/add_num", 1);

    $notification1 = new GTThirdNotification();
    $notification1->setTitle($data['title']);
    $notification1->setBody($data['content']);
    $notification1->setClickType($data['clickType']);
    if ($data['clickType'] == 'payload') {
        $notification1->setPayload($data['payload_data']);
    }
    if ($data['clickType'] == 'url') {
        $notification1->setUrl($data['url']);
    }
    if ($data['clickType'] == 'intent') {
        $notification1->setIntent($data['intent_data']);
    }
    $ups->setNotification($notification1);
    $androidDTO->setUps($ups);
    $pushChannel->setAndroid($androidDTO);
    $push->setPushChannel($pushChannel);

    //推送苹果离线通知标题内容
    $alert = new GTAlert();
    $alert->setTitle($data['title']);
    $alert->setBody($data['content']);
    $aps = new GTAps();
    //1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。
    //苹果建议1小时最多推送3条静默消息
    $aps->setContentAvailable(0);
    $aps->setSound("default");
    $aps->setAlert($alert);
    $aps->setCategory('ACTIONABLE');
    $iosDto = new GTIos();
    $iosDto->setAps($aps);
    $iosDto->setType("notify");
    $iosDto->setPayload($data['payload_data']);
    $iosDto->setAutoBadge("+1");
    $pushChannel->setIos($iosDto);
    //var_dump($iosDto->getApiParam());
    $push->setPushChannel($pushChannel);

    $push->setCid($data['cid']);
    //处理返回结果
    $result = $api->pushApi()->pushToSingleByCid($push);
    write_log(json_encode($result, JSON_UNESCAPED_UNICODE));
    write_log('----');

    var_dump($result);
    //
    if ($result['code'] == 0) {
        return true;
    }
    return false;
}

 

posted @ 2024-03-23 10:06  样子2018  阅读(627)  评论(0编辑  收藏  举报