使用带有 Laravel 的 Firebase Admin Sdk,在后台或终止状态下不显示提醒通知

heads-up notification not showing on background or terminated state using Firebase Admin Sdk with Laravel

我已将 Firebase Cloud Messaging 集成到我的 Flutter 移动应用程序中,其中包含一个名为 flutter_local_notifications 的插件,用于在应用程序的每个状态下显示提醒通知。一切正常,直到我创建了一个 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()]);
    }
}

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

我忘记将我的通知渠道 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()]);
}