当前位置: 首页 > 工具软件 > PHP-Push-2 > 使用案例 >

php接口 push,unipush服务端推送PHP版RestAPI V2 的离线/在线推送

吕森
2023-12-01

关于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);

}

 类似资料: