极光推送

来源:互联网 发布:centos mysql启动失败 编辑:程序博客网 时间:2024/05/17 23:00
/**
 * 给单个用户发送通知
 * @param  int $user_id 用户ID
 * @param string $content 发送内容
 * @param string $title 标题
 * @param int $open_type 打开类型
 * @param string $open_url 打开url
 * @param int $pipei_id 展示的数据主键根据ptype决定
 * @param int $ptype 一级通知类型
 */
public function sendToUser($user_id, $content, $title='', $open_type=0, $open_url='',$pipei_id = 0,$ptype = 0,$is_push = true)
{
    if (empty($user_id) || empty($content))
    {
        return false;
    }
    try {

        $identify = \common\models\basedata\MsgIdentify::getByUid((int)$user_id, false);
        
        if (!$identify)
        {
            return false;
        }
        $registration_id = $identify[0]['identify'];
        $platform = $identify[0]['platform'];

        $open_type = $platform == PLATFORM_IOS ?(string)$open_type : $open_type;
        $notification = ['extras' => ['open_type' => $open_type,'open_url' => $open_url]];

        $client = $this->client->push()->setPlatform($platform == PLATFORM_ANDROID ? 'android' : 'ios');
//            $client = $this->client->push()->setPlatform('all');
        if ($platform == PLATFORM_ANDROID)
        {
             $client->androidNotification($content, $notification);
        } else if ($platform == PLATFORM_IOS) {
            $client->iosNotification($content, $notification);
        }

        $notification['title'] = $title;
        $client->message($content, $notification);
        $client->setOptions((int)$user_id, MsgPush::MSG_LIVE_TIME,null,(YII_DEBUG ? false : true));
        
        $client->addRegistrationId($registration_id);
        $response = $client->send();
        if (isset($response['http_code']) && $response['http_code']==200)
        {
            $jmsg_id= $response['body']['msg_id'];
            if($is_push)
            {
                \common\models\basedata\MsgSend::add($content,$user_id,0,$jmsg_id,(int)$open_type,$open_url,$pipei_id,$ptype,$title);
            }

            return true;
        }
    }  catch (\JPush\Exceptions\APIConnectionException $e)
    {
        \common\helper\Common::dblog($e->getTraceAsString(),10003);
        return self::SEND_EXCEPTION;
    } catch (\JPush\Exceptions\APIRequestException $e)
    {
       \common\helper\Common::dblog($e->getTraceAsString(), 10003);
        return self::SEND_EXCEPTION;
    } catch (\Exception $e) {
        \common\helper\Common::dblog($e->getTraceAsString(), 10003);
        return self::SEND_EXCEPTION;
    }
    return false;

}

原创粉丝点击