个推 服务器端代码示列

您好,这里是unipush问题交流群,很高兴为您服务,在线支持时间为工作日早上9:00-12:00;下午13:30-18:00,有什么问题可以在这里交流,谢谢。

UniPush开通指南 https://ask.dcloud.net.cn/article/35716
UniPush使用指南 http://ask.dcloud.net.cn/article/35622
Unipush常见问题  https://ask.dcloud.net.cn/article/36611
服务端api接口推送调用文档
https://docs.getui.com/getui/server/rest_v2/push/


api文档 https://docs.getui.com/getui/server/rest_v2/common_args/
uniapp开发说明 https://ask.dcloud.net.cn/article/35622

自己写的code
    include("./getui-pushapi-php-client-v2-hf32/GTClient.php");

    $api = new GTClient("https://restapi.getui.com","9N6ApIQ4J79Z2zkJUErBq4", "6sK8gLODie63nMnrktFIl2","hbGt1Z8atYA3GtPXUdT3Q7");
    //设置推送参数

    $push = new GTPushRequest();
    $push->setRequestId(md5(time().mt_rand(1000,9999)));
    $pushChannel = new GTPushChannel();  

    $message = new GTPushMessage();
    $notify = new GTNotification();
    $notify->setTitle($title);
    $notify->setBody($msg);
    //点击通知后续动作,目前支持以下后续动作:
    //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
    $notify->setClickType("none");
    $message->setNotification($notify);
    $push->setPushMessage($message);
    $push->setCid("CID");

    $alert = new GTAlert();
    $alert->setTitle($title);
    $alert->setBody($msg);
    $aps = new GTAps();
    $aps->setAlert($alert);
    $iosDto = new GTIos();  
    $iosDto->setAps($aps);  
    $iosDto->setType("notify");  
    $pushChannel->setIos($iosDto);  

/*    android走个推通过,所以不需要走厂商通道更快一些。
    $androidDTO = new GTAndroid();  
    $ups = new GTUps();  
    $notification = new GTThirdNotification();;  
    $notification->setTitle($title);  
    $notification->setBody($msg);  
    $ups->setNotification($notification); 
    $androidDTO->setUps($ups);  
    $pushChannel->setAndroid($androidDTO);  
*/
    $push->setPushChannel($pushChannel); 

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

    //echo var_export($result,true);
    echo "<br>发送成功<br>";


-----------------------别人写的code-----------------------------------
1.检查参数是否正确。

2.可以先不要使用透传方式。

3.是否设置了厂商通道消息内容

4.设置为打开应用内特定页面就是intent参数。

5.intent参数是否正确

intent:#Intent;component=包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=".urlencode(标题).";S.content=".urlencode($内容).";S.payload=".json_encode(你的自定义参数).";end 



6.后台参看代码。

 $api = new GTClient("https://restapi.getui.com/","", "",""); 
 $stt = ['default'=>1]; 
 //设置推送参数 
 $push = new GTPushRequest(); 
 $push->setRequestId(time()); 
 $message = new GTPushMessage(); 
 $notify = new GTNotification(); 
 $channel = new GTPushChannel(); 

 //配置推送条件 
 $str = new GTStrategy(); 
 $str->setDefault(1); 
 $str->setHw(1); 
 $setting = new GTSettings(); //定时推送暂无 
 $setting->setStrategy($str); 
 $push->setSettings($setting); 
 $setting->setTtl(3600000); ////消息有效期,走厂商消息需要设置该值 

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

 //安卓离线厂商通道推送消息体 
 $pushChannel = new GTPushChannel(); 
 $androidDTO = new GTAndroid(); 
 $ups = new GTUps(); 
 $notification1 = new GTThirdNotification();; 
 $notification1->setTitle($title); 
 $notification1->setBody($content); 
 $ups->setNotification($notification1); 
 if($clickType == 'none') //设置推送类型 
 { 
 $notification1->setClickType($clickType); 
 }else if($clickType == 'payload' || $clickType == 'payload_custom'){ //自定义消息 打开APP和不打开APP 
 $notification1->setClickType($clickType); 
 $notification1->setPayload(json_encode($data)); 
 }else if($clickType == 'url'){ //打开URL 
 $notification1->setClickType($clickType); 
 $notification1->setUrl($url); 
 }else if($clickType == 'intent'){ //打开特定页面 
 $notification1->setClickType($clickType); 
 $notification1->setIntent("intent:#Intent;component=包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=".urlencode($title).";S.content=".urlencode($content).";S.payload=".json_encode($data).";end"); 
 }else{ 
 $notification1->setClickType($clickType); 
 } 
 //各厂商自有功能单项设置 
 if($time){ 
 $message->setDuration($time); //设置推送时间 
 $data = json_encode(array('title'=>$title,'content'=>$content,'duration'=>$time,'importance'=>'HIGH','payload'=>json_encode($data))); 
 }else{ 
 $data = json_encode(array('title'=>$title,'content'=>$content,'payload'=>json_encode($data))); 
 } 
 //$ups->setTransmission($data); 
 $androidDTO->setUps($ups); 
 $pushChannel->setAndroid($androidDTO); 
 $push->setPushChannel($pushChannel); 

 //个推在线通道 
 $notify->setTitle($title); 
 $notify->setBody($content); 
 if($img) $notify->setBigImage($img); //推送图片 
 //1、intent:打开应用内特定页面 2、url:打开网页地址。3、payload:自定义消息内容启动应用。4、payload_custom:自定义消息内容不启动应用。5、startapp:打开应用首页。6、none:纯通知,无后续动作 
 if($clickType == 'none') //设置推送类型 
 { 
 $notify->setClickType($clickType); 
 }else if($clickType == 'payload' || $clickType == 'payload_custom'){ //自定义消息 打开APP和不打开APP 
 $notify->setClickType($clickType); 
 $notify->setPayload(json_encode($data)); 
 }else if($clickType == 'url'){ //打开URL 
 $notify->setClickType($clickType); 
 $notify->setUrl($url); 
 }else if($clickType == 'intent'){ //打开特定页面 
 $notify->setClickType($clickType); 
 $notify->setIntent("intent:#Intent;component=包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=".urlencode($title).";S.content=".urlencode($content).";S.payload=".json_encode($data).";end"); 
 }else{ 
 $notify->setClickType($clickType); 
 } 
 $message->setNotification($notify); 
 $push->setPushMessage($message); 
 /* if($time){ 
 $message->setDuration($time); //设置推送时间 
 $data = json_encode(array('title'=>$title,'body'=>$content,'duration'=>$time,'importance'=>'HIGH','payload'=>json_encode($data))); 
 }else{ 
 $data = json_encode(array('title'=>$title,'body'=>$content,'payload'=>json_encode($data))); 
 } 
 $ups->setTransmission($data); // 
 $android->setUps($ups); 
 $channel->setAndroid($android); 
 $push->setPushChannel($channel); 
 $push->setPushMessage($message);*/ 
 if($cid) //推送给某个用户 
 { 
 $push->setCid($cid); 
 //处理返回结果 
 $result = $api->pushApi()->pushToSingleByCid($push); 
 }else{ 
 $result = $api->pushApi()->pushAll($push); 
 } 
 Log::info('推送数据:【推送】'.json_encode($result)); //写入日志 
 return $result;
-----------------------------------------------------
1,安装项目依赖。

        composer require getuilaboratory/getui-pushapi-php-client-v2

2,根据用户Cid单推

public function pushToSingleByCid($system = 'android'){
//创建API,APPID等配置参考 环境要求 进行获取
 $api = new \GTClient("https://restapi.getui.com","你的个推appkey", "你的个推appId","你的个推masterSecret");
 //设置推送参数
 $push = new \GTPushRequest();
 $push->setRequestId($this->micro_time());
 $message = new \GTPushMessage();
 $notify = new \GTNotification();
 $settings = new \GTSettings();
 $strategy = new \GTStrategy();
 $strategy->setDefault(\GTStrategy::STRATEGY_GT_FIRST);
 $settings->setStrategy($strategy);
 $notify->setTitle("订单提醒");
 $notify->setBody("您有新的订单,请及时处理");
 //点击通知后续动作,目前支持以下后续动作:
 //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
// $notify->setClickType("intent:#Intent;launchFlags=0x04000000;action=android.intent.action.oppopush;component=and.dcloud.Lapp/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=测试标题;S.content=测试内容;S.payload=test;end");
 $notify->setClickType("startapp");
 
 //判断一下操作系统
 if($system == "android"){
 $message->setNotification($notify); //android
 }elseif($system == "ios"){
 $message->setTransmission("您有新的订单,请及时处理"); //ios
 }

 $push->setPushMessage($message);
 $push->setSettings($settings);
 $pushChannel = new \GTPushChannel();
 //ios
 $ios = new \GTIos();
 $ios->setType("notify");
 $ios->setAutoBadge("1");
 $ios->setPayload("ios_payload");
 $ios->setApnsCollapseId($this->micro_time());
 //aps设置
 $aps = new \GTAps();
 $aps->setContentAvailable(0);
 $aps->setSound("music");
 $aps->setCategory("category");
 $aps->setThreadId("threadId");

 $alert = new \GTAlert();
 $alert->setTitle("订单提醒");
 $alert->setBody("您有新的订单,请及时处理");
 $alert->setActionLocKey("ActionLocKey");
 $alert->setLocKey("LocKey");
 $alert->setLocArgs(array("LocArgs1","LocArgs2"));
 $alert->setLaunchImage("LaunchImage");
 $alert->setTitleLocKey("TitleLocKey");
 $alert->setTitleLocArgs(array("TitleLocArgs1","TitleLocArgs2"));
 $alert->setSubtitle("");
 $alert->setSubtitleLocKey("");
 $alert->setSubtitleLocArgs(array("subtitleLocArgs1","subtitleLocArgs2"));
 $aps->setAlert($alert);
 $ios->setAps($aps);

 $multimedia = new \GTMultimedia();
 $multimedia->setUrl("url");
 $multimedia->setType(1);
 $multimedia->setOnlyWifi(false);
 $multimedia2 = new \GTMultimedia();
 $multimedia2->setUrl("url2");
 $multimedia2->setType(2);
 $multimedia2->setOnlyWifi(true);
 $ios->setMultimedia(array($multimedia));
 $ios->addMultimedia($multimedia2);
 $pushChannel->setIos($ios);
 //安卓
 $android = new \GTAndroid();
 $ups = new \GTUps();
// $ups->setTransmission("ups Transmission");
 $thirdNotification = new \GTThirdNotification();
 $thirdNotification->setTitle("订单提醒");
 $thirdNotification->setBody("您有新的订单,请及时处理");
 $thirdNotification->setClickType(\GTThirdNotification::CLICK_TYPE_STAERAPP);
 $thirdNotification->setIntent("intent:#Intent;component=and.dcloud.Lapp/io.dcloud.PandoraEntry;S.parm1=value1;S.parm2=value2;end");
 $thirdNotification->setUrl("https://www.baidu.com");
 $thirdNotification->setPayload("payload");
 $thirdNotification->setNotifyId(mt_rand(0,2147483647));
 $ups->addOption("HW","badgeAddNum",1);
 $ups->addOption("OP","channel","Default");
 $ups->addOption("OP","aaa","bbb");
 $ups->addOption(null,"a","b");

 $ups->setNotification($thirdNotification);
 $android->setUps($ups);
 $pushChannel->setAndroid($android);
 $push->setPushChannel($pushChannel);
 $push->setCid("用户cid");
 $result = $api->pushApi()->pushToSingleByCid($push);
}

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

 


发表评论

电子邮件地址不会被公开。 必填项已用*标注