极光推送 php laravel

使用 Composer 安装
在项目中的 composer.json 文件中添加 jpush 依赖:
“require”: {
“jpush/jpush”: “v3.5.*”
}
执行 composer update 或 $ composer install进行安装。

创建控制器AutoMessageControallers.php

<?php
namespace App\Http\Controllers;
use JPush\Client as JPush;
use DB;
use League\Flysystem\Exception;

class ApiAutoMessageController extends Controller
{
    public function send_message()
    {

        $r=DB::table("message")->where("status",0)->limit(100)->get();
        foreach ($r as $v)
        {
            if($this->push($v['id'],$v['user_id'],$v['message']))
            {
                DB::table("message")->where("id",$v['id'])->update(array('status'=>1));
            }
        }
    }
    private function push($id,$user_id,$message)
    {
        try
        {
        $client = new JPush("255f4c7684e2c2704de6ef83", "d51eaa6db6e5663d1ee4eec1");
        $push = $client->push();
//      $regId=array($user_id);
        $platform = array('ios', 'android');
        $alert = $message;
        $tag = array($user_id);
//            $tag = array('5');
        $ios_notification = array(
            'sound' => 'hello',
            'badge' => 2,
            'content-available' => true,
            'category' => 'jiguang',
            'extras' => array(
                'key' => 'value',
                'jiguang'
            ),
        );
        $android_notification = array(
            'title' => 'hello',
            'build_id' => 2,
            'extras' => array(
                'key' => 'value',
                'jiguang'
            ),
        );

        $content = $message;
        $message = array(
            'title' => 'hello',
            'content_type' => $message,
            'extras' => array(
                'id' => $id
            ),
        );
        $options = array(
//          'sendno' => 100,
//          'time_to_live' => 100,
//          'override_msg_id' => 100,
//          'big_push_duration' => 100
        );
        $response = $push->setPlatform($platform)
            ->addTag($tag)
            //->addRegistrationId($regId)
            ->iosNotification($alert, $ios_notification)
            ->androidNotification($alert, $android_notification)
            ->message($content, $message)
            ->options($options)->send();
            return true;
        }
        catch (Exception $e)
        {
            //print_r($e);
            return true;
        }
    }
}

调用

 $r=DB::table('annunciate')->where('a_id',$aid)->first();
  DB::table("message")->insert(
                                  array("user_id"=>$r['user_id'],"message"=>"".$r['title']."","add_time"=>time()));
  DB::table("message")->insert(
                                        array("user_id"=>$uid,"message"=>"报名".$r['title']."成功","add_time"=>time()));
//直接new
 $send = new ApiAutoMessageController();

 $send->send_message();

版权声明:本文为qq_15283821原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。