public function push($TaskList, $AppName, $XMAppSecret, $PackageName) { $secret = $XMAppSecret; $package = $PackageName; foreach ($TaskList as &$Task) { Constants::setPackage($package); Constants::setSecret($secret); $UserRegIds = array($Task->device_token); $title = $Task->title; if ($title || empty($title)) { $title = "赶集"; } $desc = $Task->bussiness_message; $payload = json_decode($Task->bussiness_data); $payload->push_message_id = $Task->id; $sender = new Sender(); // message1 演示自定义的点击行为 $message1 = new Builder(); $message1->passThrough($Task->pass_through); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数 if ($Task->pass_through != 1) { $message1->title($title); // 通知栏的title $message1->description($desc); // 通知栏的descption } $message1->payload(json_encode($payload)); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。 $message1->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0 $message1->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存 $message1->build(); $targetMessage = new TargetedMessage(); // $targetMessage->setTarget('alias1', TargetedMessage::TARGET_TYPE_ALIAS); // 设置发送目标。可通过regID,alias和topic三种方式发送 $targetMessage->setMessage($message1); // var_dump($Task->bussiness_data); $back = $sender->sendToIds($message1, $UserRegIds)->getRaw(); //var_dump($back); if ($back['code'] == 0) { $Task->done(1); } else { $Task->done(2); } } }
/** * 从redis中获取待发送Push消息,将其推送到push服务中心,然后将入库数据临时写到redis */ public function run() { echo "start run..."; $startTime = time(); while (true) { $push = \Util\Tools\PushRedis::popPushWaitSend(); if ($push['title'] && $push['userIds'] && $push['message'] && $push) { foreach ($push['userIds'] as $userId) { foreach ($push['appIds'] as $appId) { $iosTokens = \Util\Tools\PushRedis::getIosToken($appId, $userId); foreach ($iosTokens as $token) { $app = self::$_appList[$appId]; if ($app['platform'] == self::IOS) { $connection = self::_getConnection($appId); $badgeCount = $push['bubble']; $body['aps'] = array('badge' => (int) $badgeCount, 'alert' => $push['message'], 'sound' => 'default'); if ($push['data']) { $body['p'] = json_decode($push['data']); } $messageId = self::_getMessageId(); $body['ID'] = $messageId; $payload = json_encode($body); $deviceToken = $token; $notification = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; fwrite($connection, $notification, strlen($notification)); self::_insertToSQL(self::IOS, $push, $token, $userId, $appId, $messageId); } } $androidTokens = \Util\Tools\PushRedis::getAndroidToken($appId, $userId); if ($androidTokens) { $app = self::$_appList[$appId]; if ($app['platform'] == self::ANDROID) { $secret = $app['xm_app_secret']; $package = $app['package_name']; Constants::setPackage($package); Constants::setSecret($secret); $UserRegIds = $androidTokens; $title = $push['title']; if ($title || empty($title)) { $title = "赶集"; } $desc = $push['message']; $payload = json_decode($push['data']); $messageId = self::_getMessageId(); $payload->push_message_id = $messageId; $sender = new Sender(); $message1 = new Builder(); $message1->passThrough($push['pass_though']); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数 if ($push['pass_though'] != 1) { $message1->title($title); // 通知栏的title $message1->description($desc); // 通知栏的descption } $message1->payload(json_encode($payload)); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。 $message1->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0 $message1->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存 $message1->build(); $targetMessage = new TargetedMessage(); $targetMessage->setMessage($message1); $sender->sendToIds($message1, $UserRegIds)->getRaw(); foreach ($androidTokens as $token) { $state = self::_insertToSQL(self::ANDROID, $push, $token, $userId, $appId, $messageId); } } } } } } $currentTime = time(); if ($currentTime - $startTime > 178) { break; } } echo "ran end"; }
$aliasList = array('alias1', 'alias2'); $title = '你好'; $desc = '这是一条mipush推送消息'; $payload = '{"test":1,"ok":"It\'s a string"}'; $sender = new Sender(); // message1 演示自定义的点击行为 $message1 = new Builder(); $message1->title($title); // 通知栏的title $message1->description($desc); // 通知栏的descption $message1->passThrough(0); // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数 $message1->payload($payload); // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。 $message1->extra(Builder::notifyForeground, 1); // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0 $message1->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存 $message1->build(); $targetMessage = new TargetedMessage(); $targetMessage->setTarget('alias1', TargetedMessage::TARGET_TYPE_ALIAS); // 设置发送目标。可通过regID,alias和topic三种方式发送 $targetMessage->setMessage($message1); //// message2 演示预定义点击行为中的点击直接打开app行为 //$message2 = new Builder(); //$message2->title($title); //$message2->description($desc); //$message2->passThrough(0); //$message2->payload($payload); // 对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。 //$message2->extra(Builder::notifyEffect, 1); // 此处设置预定义点击行为,1为打开app
// 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0 $message1->notifyId(2); // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存 $message1->build(); $targetMessage = new TargetedMessage(); $targetMessage->setTarget('abcdefg', TargetedMessage::TARGET_TYPE_ALIAS); // 设置发送目标。可通过regID,alias和topic三种方式发送 $targetMessage->setMessage($message1); // message2 演示预定义点击行为中的点击直接打开app行为 $message2 = new Builder(); $message2->title($title); $message2->description($desc); $message2->passThrough(0); $message2->payload($payload); // 对于预定义点击行为,payload会通过点击进入的界面的intent中的extra字段获取,而不会调用到onReceiveMessage方法。 $message2->extra(Builder::notifyEffect, 1); // 此处设置预定义点击行为,1为打开app $message2->extra(Builder::notifyForeground, 1); $message2->notifyId(0); $message2->build(); $targetMessage2 = new TargetedMessage(); $targetMessage2->setTarget('abcdefg', TargetedMessage::TARGET_TYPE_ALIAS); $targetMessage2->setMessage($message2); $targetMessageList = array($targetMessage, $targetMessage2); print_r($sender->multiSend($targetMessageList, TargetedMessage::TARGET_TYPE_ALIAS)->getRaw()); #print_r($sender->sendToAliases($message1,$aliasList)->getRaw()); //$stats = new Stats(); //$startDate = '20140301'; //$endDate = '20140312'; //print_r($stats->getStats($startDate,$endDate)->getData()); //$tracer = new Tracer();