我正在使用 Laravel 作为我的应用程序后端,并希望按主题向我的 flutter 应用程序发送推送通知。现在我在我的 flutter 应用程序中实现了 firebase 消息传递。作为
_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');
});
}
我正在通过邮递员向应用程序发送通知,它正在工作。在此输入图像描述 https://i.stack.imgur.com/F5p6c.png现在请告诉我如何从 Laravel 表单(从视图目录)发送通知。
我在资源目录中创建了一个名为 PushNotification 的控制器和一个视图目录(\resources\views\notification\create.blade)。
如果您有控制器设置,那么从前端/视图发送通知就不那么困难了。这是我的完整示例。
-
在视图 form.blade.php 文件 (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)
Route::get('form', function () {
return view('form');
});
Route::post('send','MyController@bulksend')->name('bulksend');
-
在 app/Http/Controller 中创建一个名为 MyController 的控制器,并将此函数添加到其中。
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)