使用 Firebase Admin Sdk 和 Laravel 时,抬头通知未显示在后台或终止状态
我已使用名为 flutter_local_notifications 的插件将 Firebase Cloud Messaging 集成到我的 Flutter 移动应用程序中,用于在应用程序的每个状态下显示抬头通知。一切都工作正常,直到我创建了一个 Laravel 项目以使用设备令牌向特定设备发送通知。
我已将 Laravel 项目与 Firebase Admin SDK 集成,并遵循使用云消息传递的文档。目前,平视通知仅在前台状态下显示,在后台或终止状态下不显示。
这是我的测试 Laravel 控制器
public function sendFCM()
{
$deviceToken = 'device token';
$title = 'My Notification Title';
$body = 'My Notification Body';
$imageUrl = 'http://lorempixel.com/400/200/';
$notification = Notification::fromArray([
'title' => $title,
'body' => $body,
'image' => $imageUrl,
]);
$notification = Notification::create($title, $body);
$data = ['route' => 'resultipt'];
$message = CloudMessage::withTarget('token', $deviceToken)
->withNotification($notification)
->withData($data)
->withHighestPossiblePriority();
try {
$this->messaging->send($message);
return response()->json(['status' => 'Successfully sent']);
} catch (InvalidMessage $th) {
return response()->json(['status' => $th->getMessage()]);
}
}
I have integrated Firebase Cloud Messaging to my Flutter mobile app with a plugin called flutter_local_notifications for showing heads-up notification in every state of the app. Everything is working fine until I created a Laravel project for sending notification to the specific device using device token.
I have integrated the Laravel project with Firebase Admin SDK and I follow the documentation for using Cloud Messaging. For now, the heads-up notification only showing in foreground state and not showing in background or terminated state.
Here is my testing laravel controller
public function sendFCM()
{
$deviceToken = 'device token';
$title = 'My Notification Title';
$body = 'My Notification Body';
$imageUrl = 'http://lorempixel.com/400/200/';
$notification = Notification::fromArray([
'title' => $title,
'body' => $body,
'image' => $imageUrl,
]);
$notification = Notification::create($title, $body);
$data = ['route' => 'resultipt'];
$message = CloudMessage::withTarget('token', $deviceToken)
->withNotification($notification)
->withData($data)
->withHighestPossiblePriority();
try {
$this->messaging->send($message);
return response()->json(['status' => 'Successfully sent']);
} catch (InvalidMessage $th) {
return response()->json(['status' => $th->getMessage()]);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了我自己的问题。
我忘记将我的通知通道 ID 添加到 Android 配置中。
I have solved my own problem.
I forgot to add my notification channel id to the android config.