Dhiemas Ganisha
Dhiemas Ganisha

Reputation: 223

FCM Config Problem Push Notif Android & iOs in Laravel

I got some problems with FCM in my projects.

This is my config FCM in .env

FCM_SERVER_KEY_ANDRO=thisKeyForAndroid
FCM_SERVER_KEY_IOS=thisKeyForIOS
FCM_SENDER_ID=senderId

And this is my config 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
    ],
];

I have a problem when I use FCM_SERVER_KEY_ANDRO for server_key, push notification in IOS is not working. But when I use FCM_SERVER_KEY_IOS for server_key, push notification in IOS is working but not in Android. I can't for adding new parameter on http for key android or key ios.

I want FCM_SERVER_KEY_ANDRO and FCM_SERVER_KEY_IOS to run concurrently on fcm.php, but i don't know how to run both?

Sorry for my bad grammar

Upvotes: 1

Views: 1014

Answers (1)

Zaqi Salman Alexa
Zaqi Salman Alexa

Reputation: 70

you can use config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]); for android and config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]); for iOS but you need to separate the function to make this work. you can put this code before $downstreamResponse = FCM::sendTo($token, $option, $notification, $data);

Here the example:

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);
}

Upvotes: 1

Related Questions