极光推送

/**
 * 给单个用户发送通知
 * @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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值