+
95
-

回答

unipush中可以设置华为的消息分类,分类是为了规范消息的发送,可以申请消息分类,几乎所有场景都覆盖,但是每个场景有自己的消息规则,比如IM即时通讯,消息发送格式就是title:"小明",content:"小明说:你是谁?",而且在options中还要传递catagory值,下面我贴出全部的测试通过的代码

1、先下载sdk,我们以php为例,是个推2.0的restfullapi实现的。

下载地址:https://down.bfw.wiki/file/17027139339621670052.html

2、开通unipush1.0,记住是1.0.,unipush2.0是采用unicloud的发送方式,必须要买unicloud的服务器才能使用,而unipush1.0还是可以使用restfull的方式实现,申请1.0成功后就会有appid及secret等参数,记下来。

3、php代码实现华为离线分类消息推送

点击查看全文

<?php

header("Content-Type: text/html; charset=utf-8");


function micro_time() {
    list($usec, $sec) = explode(" ", microtime());
    $time = ($sec . substr($usec, 2, 3));
    return $time;
}

try {


    require_once(dirname(__FILE__) . '/' . 'GTClient.php');


    define('APPKEY', '');// 从unipush获取
    define('APPID', '');// 从unipush获取
    define('MASTERSECRET', '');// 从unipush获取
    define('CID', '');//cid

    define("URL", "https://restapi.getui.com");




    $token = null;
    $taskId = null;
    $api = new GTClient(URL, APPKEY, APPID, MS);

    pushToSingleByCid("李好", "李好:你什么时候出发呢", "123:123123");


} catch (Exception $e) {
    echo $e->getMessage();
}




function pushToSingleByCid($title, $content, $payload) {

    $api = new GTClient(URL, APPKEY, APPID, MASTERSECRET);
    //设置推送参数
    $push = new GTPushRequest();
    $push->setRequestId(micro_time());
    $message = new GTPushMessage();
   
    $notify = new GTNotification();
    $notify->setTitle($title);
    $notify->setBody($content);

    $notify->setClickType("startapp");
    $message->setNotification($notify);

    $pushChannel = new GTPushChannel();
    $android = new GTAndroid();
    $ups = new GTUps();
   
   //华为的额外参数传递
    $ups->addOption("HW", "/message/android/category", "IM");


    $thirdNotification = new GTThirdNotification();
    $thirdNotification->setTitle($title);
    $thirdNotification->setBody($content);
    $thirdNotification->setClickType(GTThirdNotification::CLICK_TYPE_INTENT);

    $thirdNotification->setIntent("intent://io.dcloud.unipush/?#Intent;scheme=你app的schemes;launchFlags=0x4000000;component=你的包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=".$title.";S.content=".$content.";S.payload=".$payload.";end");

    $thirdNotification->setPayload($payload);
    $thirdNotification->setNotifyId(rand(10000, 100000));

    $ups->setNotification($thirdNotification);

    $android->setUps($ups);
    $pushChannel->setAndroid($android);
    $push->setPushChannel($pushChannel);
    $push->setPushMessage($message);
    $push->setCid(CID);


    //处理返回结果
    $result = $api->pushApi()->pushToSingleByCid($push);
    var_dump($result);
}

?>

uniapp的前端代码就是,在App.vue的onlaunch中

uni.onPushMessage((res)=>{
				console.log(res)
			})

推动app离线通知后打开app的获取数据是

{
    "type": "click",
    "data": {
        "__UUID__": "androidPushMsg14296456",
        "title": "李好",
        "appid": "__UNI__0A110CF",
        "content": "李好:你什么时候出发呢",
        "payload": "123:123123"
    }
}

如果仅仅传递unipush的透传消息,不进行通知栏通知,代码这样

<?php
//透传消息发送
function pushToSingleByCidTrans($trans) {

    $api = new GTClient(URL, APPKEY, APPID, MASTERSECRET);
    //设置推送参数
    $push = new GTPushRequest();
    $push->setRequestId(micro_time());
    $message = new GTPushMessage();
    $message->setTransmission($trans);
   

    $push->setPushMessage($message);
    $push->setCid(CID3);


    //处理返回结果
    $result = $api->pushApi()->pushToSingleByCid($push);
    var_dump($result);
}

网友回复

我知道答案,我要回答