| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace common\components;
- use GTAlert;
- use GTAndroid;
- use GTAps;
- use GTClient;
- use GTIos;
- use GTMultimedia;
- use GTNotification;
- use GTPushChannel;
- use GTPushMessage;
- use GTPushRequest;
- use GTSettings;
- use GTStrategy;
- use GTThirdNotification;
- use GTUps;
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\GuzzleException;
- use Yii;
- class pushNotice
- {
- const domainUrl = 'https://restapi.getui.com';
- const appKey = 'uC5ZdrWSV48cLRBJWMSId9';
- const appId = 'YjALucIHmI7DylcjlMZEx2';
- const masterSecret = 'zSbdBLOQrxAa5IcDNqIne7';
- public static function push($adminId, $cId, $title, $body)
- {
- $notify = new GTNotification();
- $notify->setTitle($title);
- $notify->setBody($body);
- //点击通知后续动作,目前支持以下后续动作:
- //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
- $notify->setClickType("startapp");
- //保证有振动
- $notify->setChannelId('channelId1');
- $notify->setChannelName('channelName1');
- $notify->setChannelLevel(4);
- $message = new GTPushMessage();
- $message->setNotification($notify);
- //设置推送参数
- $push = new GTPushRequest();
- $time = time();
- $rand = rand(1000, 9999);
- $rId = $time . $rand . $adminId;
- $push->setRequestId($rId);
- $push->setPushMessage($message);
- $push->setCid($cId);
- //创建API,APPID等配置参考 环境要求 进行获取
- $api = new GTClient(self::domainUrl, self::appKey, self::appId, self::masterSecret);
- $api->pushApi()->pushToSingleByCid($push);
- }
- public static function gtPush($cId, $title, $body)
- {
- Yii::info('start -- push/single/cid');
- $set = new GTSettings();
- $set->setTtl(3600000);
- $strategy = new GTStrategy();
- $strategy->setDefault(GTStrategy::STRATEGY_THIRD_FIRST);
- // $strategy->setIos(GTStrategy::STRATEGY_GT_ONLY);
- // $strategy->setOp(GTStrategy::STRATEGY_THIRD_FIRST);
- // $strategy->setHw(GTStrategy::STRATEGY_THIRD_ONLY);
- $strategy->setHw(GTStrategy::STRATEGY_GT_FIRST);
- $set->setStrategy($strategy);
- //设置推送参数
- $push = new GTPushRequest();
- $push->setRequestId(self::micro_time());
- $push->setSettings($set);
- $message = new GTPushMessage();
- $notify = new GTNotification();
- $notify->setTitle($title);
- $notify->setBody($body);
- //$notify->setChannelId('channelId1');
- //$notify->setChannelName('channelName1');
- $notify->setChannelLevel(4);
- //点击通知后续动作,目前支持以下后续动作:
- //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
- $notify->setClickType("startapp");
- $message->setNotification($notify);
- $push->setPushMessage($message);
- $push->setCid($cId);
- //厂商推送消息参数
- $pushChannel = new GTPushChannel();
- // ------------- ios ---------------
- $ios = new GTIos();
- $ios->setType("notify");
- $ios->setAutoBadge("1");
- $ios->setPayload("ios_payload");
- $ios->setApnsCollapseId("apnsCollapseId");
- //aps设置
- $aps = new GTAps();
- $aps->setContentAvailable(0);
- $aps->setSound("com.gexin.ios.silenc");
- $aps->setCategory("category");
- $aps->setThreadId("threadId");
- $alert = new GTAlert();
- $alert->setTitle($title);
- $alert->setBody($body);
- $alert->setActionLocKey("ActionLocKey");
- $alert->setLocKey("LocKey");
- $alert->setLocArgs(array("LocArgs1","LocArgs2"));
- $alert->setLaunchImage("LaunchImage");
- $alert->setTitleLocKey("TitleLocKey");
- $alert->setTitleLocArgs(array("TitleLocArgs1","TitleLocArgs2"));
- $alert->setSubtitle("Subtitle");
- $alert->setSubtitleLocKey("SubtitleLocKey");
- $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);
- // ------------- ios end ---------------
- // ----------------- 安卓 -----------------
- $android = new GTAndroid();
- $ups = new GTUps();
- //$ups->setTransmission("ups Transmission");
- $ups->addOption("HW","badgeAddNum",1);
- //$ups->addOption("OP","channel","Default");
- //$ups->addOption("OP","aaa","bbb");
- //$ups->addOption(null,"a","b");
- $thirdNotification = new GTThirdNotification();
- $thirdNotification->setTitle($title);
- $thirdNotification->setBody($body);
- $thirdNotification->setClickType(GTThirdNotification::CLICK_TYPE_STAERAPP);
- //$thirdNotification->setIntent("intent:#Intent;component=你的包名/你要打开的 activity 全路径;S.parm1=value1;S.parm2=value2;end");
- //$thirdNotification->setUrl("http://docs.getui.com/getui/server/rest_v2/push/");
- //$thirdNotification->setPayload("payload");
- $thirdNotification->setNotifyId(time());
- $ups->setNotification($thirdNotification);
- $android->setUps($ups);
- $pushChannel->setAndroid($android);
- // ----------------- 安卓 end -----------------
- $push->setPushChannel($pushChannel);
- //创建API,APPID等配置
- $api = new GTClient(self::domainUrl, self::appKey, self::appId, self::masterSecret);
- //处理返回结果
- $result = $api->pushApi()->pushToSingleByCid($push);
- Yii::info('push/single/cid: ' . json_encode($result));
- }
- public static function micro_time()
- {
- list($usec, $sec) = explode(" ", microtime());
- $time = ($sec . substr($usec, 2, 3));
- return $time;
- }
- }
|