Laravel 中的 FCM 配置问题推送通知 Android & iOs

FCM Config Problem Push Notif Android & iOs in Laravel

我在项目中遇到了一些 FCM 问题。

这是我在 .env 中的配置 FCM

FCM_SERVER_KEY_ANDRO=thisKeyForAndroid
FCM_SERVER_KEY_IOS=thisKeyForIOS
FCM_SENDER_ID=senderId

这是我的配置fcm.php

<?php

return [
    'driver' => env('FCM_PROTOCOL', 'http'),
    'log_enabled' => false,

    'http' => [
        'server_key' => env('FCM_SERVER_KEY_ANDRO'), //the problem is here
        'sender_id' => env('FCM_SENDER_ID'),
        'server_send_url' => 'https://fcm.googleapis.com/fcm/send',
        'server_group_url' => 'https://android.googleapis.com/gcm/notification',
        'timeout' => 30.0, // in second
    ],
];

我在为 server_key 使用 FCM_SERVER_KEY_ANDRO 时遇到问题,IOS 中的推送通知不起作用。但是,当我将 FCM_SERVER_KEY_IOS 用于 server_key 时,IOS 中的推送通知有效,但 Android 中无效。 我不能在 http 上为键 android 或键 ios 添加新参数。

我想 FCM_SERVER_KEY_ANDRO 和 FCM_SERVER_KEY_IOS 在 fcm.php 上同时 运行,但我不知道如何 运行 两者?

抱歉我的语法不好

您可以将 config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]); 用于 android,将 config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]); 用于 iOS,但您需要将函数分开才能使其正常工作。你可以把这段代码放在 $downstreamResponse = FCM::sendTo($token, $option, $notification, $data);

之前

这里是例子:

public function ios($data, $notifications, $token)
{
    config(['fcm.http.server_key' => env('FCM_SERVER_KEY_IOS')]);
    ......
    ......
    $downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
    return response()->default(200, 'Sent', $downstreamResponse);
}

public function android($data, $notifications, $token)
{
    config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]);
    ......
    ......
    $downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
    return response()->default(200, 'Sent', $downstreamResponse);
}