叶凌风
叶凌风
  • 发布:2021-03-09 14:15
  • 更新:2022-11-09 22:28
  • 阅读:1830

unipush服务端推送PHP版RestAPI V2 的离线/在线推送

分类:uni-app

关于unipush的推送还是用个推的文档和SDK,下载个推的SDK整合进PHP框架里,然后开始表演了。刚开始在网上找真的都是老版本的,RestAPI V2 的几乎没有涉及,旧SDK的可能以后都要不支持了,所以建议大家更换新版本的SDK。话不多说,上代码:

$title为推送标题,$content为推送内容,$payload为点击通知加自定义消息,$cid为推送cid,$package为APP包名,"APPKEY","APPID","MasterSecret"这几个要传入你真实的。以下是单推例子,更多推送就不多说了。  
function pushToSingleByCid($title,$content,$payload,$cid,$package){  
    //创建API,APPID等配置参考 环境要求 进行获取  
    $api = new \GTClient_unipush("https://restapi.getui.com","APPKEY","APPID","MasterSecret");  
    //设置推送参数  
    $push = new \GTPushRequest();  
    $osn = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);  
    $push->setRequestId((string)$osn);  

    $message = new \GTPushMessage();  
      $channel = new \GTPushChannel();  
    $notify = new \GTNotification();  
  $thirdnotify = new \GTThirdNotification();  
   $ups = new \GTUps();  
    $gtAndroid = new \GTAndroid();  
    $notify->setTitle($title);  
    $notify->setBody($content);  

    $thirdnotify->setTitle($title);  
    $thirdnotify->setBody($content);  
    if(is_array($payload))  
    $pj =  json_encode($payload);  
    else  
     $pj = $payload;  
    $notify-> setPayload($pj);  
   $thirdnotify-> setPayload($pj);  

       $intent = "intent:#Intent;launchFlags=0x14000000;action=android.intent.action.oppopush;component={$package}/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={$title};S.content={$content};S.payload={$pj};end";  
         $notify->setClickType("intent");  
   $thirdnotify->setClickType("intent");  
    $notify->setIntent($intent);  
     $thirdnotify->setIntent($intent);  
    echo  $intent;  

    //点击通知后续动作,目前支持以下后续动作:  
    //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作  

  $notify->setIntent($intent);  
     // $notify->setChannelLevel(3);  

 // $touchuan=['title'=>$title,'content'=>$content,'payload'=>$package];  

//$message->setTransmission(json_encode($touchuan));//个推透传  
$message->setNotification($notify);  
 $upsback= $ups->setNotification($thirdnotify);  
 // $upsback= $ups->setTransmission(json_encode($touchuan));//厂商透传  

$gtAndroid->setUps($ups);  
$channel->setAndroid($gtAndroid);  
$push->setPushMessage($message);  

      $push->setPushChannel( $channel);  
    $push->setCid($cid);  
    //处理返回结果  
    $result = $api->pushApi()->pushToSingleByCid($push);  
  // print_r($result);  
}  
0 关注 分享

要回复文章请先登录注册

叶凌风

叶凌风 (作者)

回复 爱得源码 :
有的,你看下面的评论,一次运行成功,不知道现在的有没有
2022-11-09 22:28
爱得源码

爱得源码

uniapp的push v2 好像没有MasterSecret吧
2022-10-31 18:47
灵光一闪

灵光一闪

感谢,一次运行成功
2022-01-11 22:59
叶凌风

叶凌风 (作者)

回复 1***@qq.com :
下载SDK整合进TP啊
2021-03-19 00:58
1***@qq.com

1***@qq.com

回复 叶凌风 :
为啥我集成tp5 用你的代码测试 在线离线都收不到通知消息呢?
2021-03-17 18:27
1***@qq.com

1***@qq.com

回复 叶凌风 :
你好
2021-03-17 18:26
叶凌风

叶凌风 (作者)

GTClient_unipush原本是GTClient类,由于我的项目TP框架有同名类名,所以改成了这个
2021-03-17 14:36