本文介绍了带有Laravel的Flutter FCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Laravel用于我的App后端,并希望按主题将推送通知发送到我的flutter应用.现在,我在自己的flutter应用程序中实现了Firebase消息传递.

I am Using Laravel for my App backend and want to send push notification to my flutter app by topic. Now I implemented firebase messaging into my flutter app. as

_registerOnFirebase() {
_firebaseMessaging.subscribeToTopic('all');
_firebaseMessaging.getToken().then((token) => print(token));
 }

void getMessage() {
_firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async {
      print('received message');

    }, onResume: (Map<String, dynamic> message) async {
  print('on resume $message');

}, onLaunch: (Map<String, dynamic> message) async {
  print('on launch $message');

});
}

,并且我正在通过邮递员将通知发送到该应用,并且该通知正在运行.在此处输入图片描述现在,请告诉我如何从Laravel表单(从Views目录)发送通知.我已经在(\ resources \ views \ notification \ create.blade)的资源目录中创建了一个名为PushNotification的控制器和一个views目录.

and I am sending the notification to the app by Postman and It's working.enter image description hereNow please tell me How can I send the notification from my Laravel Forms(From Views Directory).I have created a controller Named PushNotification and a views directory in the resource directory as (\resources\views\notification\create.blade).

推荐答案

如果您具有控制器设置,那么从前端/视图发送通知就不会那么困难.这是我完整的示例.

If you have the controller setup then it won't be that tough to send notification from the frontend/views. Here is my complete example.

  1. 在您的视图form.blade.php文件中创建一个表单(resources/views/form.blade.php)

  1. Create a form in your view form.blade.php file (resources/views/form.blade.php)

 <form method="POST" action="{{route('bulksend')}}">
     <label>Title</label>
     <input type="text" hint="Title" name="title">
     <br>
     <label>Body</label>
     <input type="text" hint="Body" name="body">
     <br>
     <label>Image URL</label>
     <input type="text" hint="Image URL" name="img">
     <br>
     <label>ID</label>
     <input type="text" hint="Image URL" name="id">
     <br>
     <input type="submit"/>
 </form>

  • 创建网络路由(routes/web.php)

  • Create a web route (routes/web.php)

     Route::get('form', function () {
         return view('form');
     });
    
     Route::post('send','MyController@bulksend')->name('bulksend');
    

  • 在app/Http/Controller中创建一个名为MyController的控制器,并将此功能添加到其中.

  • Create a controller named MyController in app/Http/Controller and add this function to it.

     public function bulksend(Request $req){
         $url = 'https://fcm.googleapis.com/fcm/send';
         $dataArr = array('click_action' => 'FLUTTER_NOTIFICATION_CLICK', 'id' => $req->id,'status'=>"done");
         $notification = array('title' =>$req->title, 'text' => $req->body, 'image'=> $req->img, 'sound' => 'default', 'badge' => '1',);
         $arrayToSend = array('to' => "/topics/all", 'notification' => $notification, 'data' => $dataArr, 'priority'=>'high');
         $fields = json_encode ($arrayToSend);
         $headers = array (
             'Authorization: key=' . "YOUR_FCM_KEY",
             'Content-Type: application/json'
         );
    
         $ch = curl_init ();
         curl_setopt ( $ch, CURLOPT_URL, $url );
         curl_setopt ( $ch, CURLOPT_POST, true );
         curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
         curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
         curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
    
         $result = curl_exec ( $ch );
         //var_dump($result);
         curl_close ( $ch );
         return $result;
    
     }
    

  • 这篇关于带有Laravel的Flutter FCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-30 03:32