使用 Firebase Admin Sdk 和 Laravel 时,抬头通知未显示在后台或终止状态

发布于 2025-01-12 15:01:00 字数 1166 浏览 0 评论 0原文

我已使用名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹎☆浅夏丿初晴 2025-01-19 15:01:00

我已经解决了我自己的问题。

我忘记将我的通知通道 ID 添加到 Android 配置中。

$deviceToken = 'device token here';

$title = "My Notification Title";
$body = "My Notification Body";
$channel_id = "channel id here";

$notification = Notification::fromArray([
    'title' => $title,
    'body' => $body,
]);

$config = AndroidConfig::fromArray([
    'priority' => 'high',
    'notification' => [
        'channel_id' => $channel_id,
        'color' => '#42A5F5',
        'sound' => 'default',
    ],
]);

$notification = Notification::create($title, $body);

$data = ['route' => 'resultipt'];

$message = CloudMessage::withTarget('token', $deviceToken)
    ->withAndroidConfig($config)
    ->withNotification($notification)
    ->withData($data);

try {
    $this->messaging->send($message);
    return response()->json(['status' => 'Successfully sent', 'message' => $message]);
} catch (InvalidMessage $th) {
    return response()->json(['status' => $th->getMessage()]);
}

I have solved my own problem.

I forgot to add my notification channel id to the android config.

$deviceToken = 'device token here';

$title = "My Notification Title";
$body = "My Notification Body";
$channel_id = "channel id here";

$notification = Notification::fromArray([
    'title' => $title,
    'body' => $body,
]);

$config = AndroidConfig::fromArray([
    'priority' => 'high',
    'notification' => [
        'channel_id' => $channel_id,
        'color' => '#42A5F5',
        'sound' => 'default',
    ],
]);

$notification = Notification::create($title, $body);

$data = ['route' => 'resultipt'];

$message = CloudMessage::withTarget('token', $deviceToken)
    ->withAndroidConfig($config)
    ->withNotification($notification)
    ->withData($data);

try {
    $this->messaging->send($message);
    return response()->json(['status' => 'Successfully sent', 'message' => $message]);
} catch (InvalidMessage $th) {
    return response()->json(['status' => $th->getMessage()]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文