kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK

Home Page:https://github.com/kreait/firebase-php

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fcm_options link not working

dong-vh opened this issue · comments

My function here
public function pushNotificationWithLink(Request $request)
{
try {
$config = WebPushConfig::fromArray([
'notification' => [
'title' => '$GOOG up 1.43% on the day',
'body' => '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
'icon' => 'https://my-server/icon.png',
],
'fcm_options' => [
'link' => 'https://5tmusic.vn',
],
]);

        $title = 'My Notification Title';
        $body = 'My Notification Body';
        $imageUrl = 'http://lorempixel.com/400/200/';
        $notification = Notification::create($title, $body);

        $data = [
            'first_key' => 'First Value',
            'second_key' => 'Second Value',
        ];

        $message = CloudMessage::withTarget('topic', 'TopicMeoMeo')
            ->withNotification($notification) // optional
            ->withData($data) // optional
        ;


        $message = $message->withWebPushConfig($config);

        $this->messaging->send($message);

        return response()->json([
            'code' => Response::HTTP_CREATED,
            'success' => true,
            'message' => "Push notification with link successfully"
        ], Response::HTTP_CREATED);

    } catch (Exception $exception) {

        $exceptionMessage = $exception->getMessage();

        return response()->json([
            'code' => Response::HTTP_BAD_REQUEST,
            'success' => false,
            'message' => "Push notification with link failed with message $exceptionMessage"
        ], Response::HTTP_BAD_REQUEST);
    }
}

It still pushes the notification but when I click on the notification it doesn't redirect. Can you check for me. Thanks !

Hi, and thanks for raising this issue - unfortunatelym there is not much I can check for you. Your message composition looks good, and Firebase accepts the message, but how this is handled in the browser/web app is out of the SDK's control here.

As reference (but you certainly have looked this up already)

In the second link, I find this passage interesting:

If the link value points to a page that is already open in a browser tab, a click on the notification brings that tab into the foreground. If the page is not already open, a notification click opens the page in a new tab.

Because data messages don't support fcm_options.link, you are recommended to add a notification payload to all data messages. Alternatively, you can handle notifications using the service worker.

To debug this further, you could enable HTTP debug logging (https://github.com/kreait/laravel-firebase/blob/main/config/firebase.php#L151) and check if the sent JSON message contains everything it should.

If nothing helps (checking the sent messages, checking the handling in the receiving browser/operating system), I believe it would be best to ask in the community for the client-side library you're using, or open a support ticket with Firebase directly at https://firebase.google.com/support

🤞