public function send($device_tokens, $msg_title = '你收到一个新消息', $msg_property = []) { $data = ['platform' => ['android'], 'audience' => ['registration_id' => $device_tokens], 'notification' => ['android' => ['alert' => $msg_title, 'extras' => $msg_property]]]; $ret = $this->_send($data); //jpush推送发送出去即代表成功,不保证可达性(因为无法返回未达用户) if (isset($ret['sendno']) && $ret['sendno'] >= 0) { return Push::ret(Push::ERR_OK); } else { if (isset($ret['error'])) { return Push::ret(Push::ERR_ANDROID_SEND_FAIL . $ret['error']['code'], $ret['error']['message'], $device_tokens); } else { return Push::ret(Push::ERR_ANDROID_SEND_FAIL, 'jpush发送失败:' . json_encode($ret), $device_tokens); } } }
/** * 发送消息到苹果设备 * @param array $tokens 设备id * @param string $text 消息标题 * @param array $customProperty 自定义属性 * @param array $badget 消息在设备显示的红点中的数值 * @param string $sound 声音 * @throws \Exception * @return array 正常,返回空数组;异常,返回['tokens'=>'err msg']这种格式的错误数组 */ public function send($tokens = null, $msg_title = '你收到一个新消息', $msg_property = [], $badget = [], $sound = 'default') { try { $this->push->connect(); $invaliduser = []; $err = []; foreach ($tokens as $token) { try { $message = new \ApnsPHP_Message($token); $message->setCustomIdentifier($token); //设置消息的小图标(红点中显示的数目 $message->setBadge(isset($badget[$token]) ? intval($badget[$token]) : 1); //设置消息显示的标题 $message->setText($msg_title); // Play the default sound $message->setSound($sound); // Set a custom property foreach ($msg_property as $key => $value) { $message->setCustomProperty($key, $value); } $message->setExpiry(self::MSG_EXPIRY); // Add the message to the message queue $this->push->add($message); } catch (\ApnsPHP_Message_Exception $e) { $invaliduser[] = $token; $err[$token] = $e->getMessage(); } } // Send all messages in the message queue $this->push->send(); $this->push->disconnect(); } catch (\ApnsPHP_Exception $e) { return Push::ret(Push::ERR_IOS_SEND_FAIL, $e->getMessage()); } $aErrorQueue = $this->push->getErrors(); if (!empty($aErrorQueue)) { foreach ($aErrorQueue as $err) { $invaliduser[] = $err['MESSAGE']->getCustomIdentifier(); $err[$err['MESSAGE']->getCustomIdentifier()] = json_encode($err['ERRORS']); } } if (empty($err)) { return Push::ret(Push::ERR_OK, 'ok', []); } else { return Push::ret(Push::ERR_OK, json_encode($err), $invaliduser); } }