Laravel 中推送通知中的额外数据传递

Extra data pass in Push notification in Laravel

我正在使用 Laravel 5.3。我想在我的推送通知中发送一些额外的数据。

我正在使用 davibennun/laravel-push-notification

代码

public function sendPushToUser($user_id, $push_message,$msg_type = "chat"){

    try{

        $user = User::findOrFail($user_id);

        if($user->device_token != ""){

            if($user->device_type == 'ios'){

                return \PushNotification::app('IOSUser')
                    ->to($user->device_token)
                    ->send($push_message);

            }elseif($user->device_type == 'android'){

                return \PushNotification::app('AndroidUser')
                    ->to($user->device_token)
                    ->send($push_message);

            }
        }

    } catch(Exception $e){
        return $e;
    }

}

我想通过 $msg_type$push_message.

将来,可能会在 Notification.So 中传递更多数据,我如何传递。

send() 没有得到超过一个参数。

谢谢。

通过查看 code ,发送方法有一个选项参数:

function send($message, $options = array()) {
   $push = new Push($this->adapter, $this->addressee, ($message instanceof Message) ? $message : new Message($message, $options));
   ....
}

所以您可以像消息对象中描述的那样发送一组选项:

$message = PushNotification::Message('Message Text',array(
    'badge' => 1,
    'sound' => 'example.aiff',

    'actionLocKey' => 'Action button title!',
    'locKey' => 'localized key',
    'locArgs' => array(
        'localized args',
        'localized args',
    ),
    'launchImage' => 'image.jpg',

    'custom' => array('custom data' => array(
        'we' => 'want', 'send to app'
    ))
));

所以你可以使用:

\PushNotification::app('AndroidUser')
                ->to($user->device_token)
                ->send($push_message, array(
                  'custom' => array(
                    'custom_data1' => VALUE,
                    'custom_data2' => VALUE
                   )
                 ));
use this package: composer require edujugon/push-notification

    $this->user_id ="999";
    $this->room_id= "Fqq123;
    $this->text= "test";
    $this->appointment_id= "1";

  $this->deviceAndroid= ['d7uRUH5lQ3eydCesO8XtH0:APA91bGmCA_1UqTdIuhtbv4wt-j6JCfjnAVDrVRSC2wNkafas9XMHjvCno-eE4_msZRJKakko4l0U7v2i-IvFnSMGv_SZSovWiB7_hUjynKD6QevzkJwMsA0_H6sz8UYvnUWzp8_f'];
    
  $this->deviceIos=['d7uRUH5lQ3eydCesO8XtH0:APA91bGmCA_1UqTdIuhtbv4wt-j6JCfjnAVDrVRSC2wNkafas9XMKZzHjvCno-eE4_msZRJKakko4l0U7v2i-IvFnSMGv_SZSovWiB7_hUjynKD6QevzkJwMsA0_H6sz8UWzp8_f'];

        //send for android device
        if(count($this->deviceAndroid)){
            $this->device_token= $this->deviceAndroid;
            $this->sendAndroidNotification();
        }

         //send for ios device
         if(count($this->deviceIos)){
            $this->device_token=$this->deviceIos;
            $this->sendIosNotification();
        }
    public function sendAndroidNotification(){

        try {
            $push = new PushNotification('fcm');
            $response   =   $push->setMessage([
        // 'notification' => [
        //         'title'=>'My Appointment',
        //         'body'=> $this->request->text ?? '',
        //         'sound' => 'default'
        //         ],
        'data' => [
                'title'=>'My Appointment',
                'body'=> $this->text ?? '',
                'sound' => 'default',
                'user_id' =>  $this->user_id ?? '',
                'room_id' =>  $this->room_id ?? '',
                'text' =>  $this->text ?? '',
                'appointment_id' =>  $this->appointment_id ?? '',
                'type' => "appointment_notification",
                ]
        ])
      
        ->setApiKey(env('PUSHER_APP_KEY_CUSTOME'))
        ->setDevicesToken($this->device_token)
        ->send()
        ->getFeedback();
      

        Log::info( collect($response)->toArray());

        } catch (Exception $e) {
            Log::info( $e);
            // return $e;

    }
   
    }

    public function sendIosNotification(){
         try {
            $push = new PushNotification('apn');
            $response  =  $push->setMessage([
            'aps' => [
                'alert'=>$this->request->text ?? '',
                'sound' => 'default',
                'badge' => 1
            ],'extraPayLoad' => [
                'user_id' =>  $this->user_id ?? '',
                'room_id' =>  $this->room_id ?? '',
                'text' =>  $this->text ?? '',
                'appointment_id' =>  $this->appointment_id ?? '',
                'type' => "appointment_notification",
            ]
        ])
        ->setConfig([
            'passPhrase' => env('PATIENT_IOS_CERTIFICATE_PASSWORD'),
            'certificate' =>config_path(). "/iosCertificates/".env('PATIENT_IOS_CERTIFICATE_NAME')
        ])
          ->setApiKey(env('PUSHER_APP_KEY_CUSTOME'))
          ->setDevicesToken($this->device_token)
          ->send()
          ->getFeedback();
     

          Log::info( collect($response)->toArray());
        //   dd( $response);
 
        } catch (Exception $e) {
            Log::info( $e);
        }
    }