mercredi 17 février 2021

PHP: I can't send a custom URL in FCM Background

I am trying to send a request to Firebase API, In the beginning, I sent a request with the same domain that my PHP file on it. for example, if my PHP script that I post the request on it like this: https://mydomainproject.com

in the JSON data will look like this:

    {
    "to" : "<TOKEN>"
    , 

    "notification": {
    "title": "test",
    "body": "Firebase Cloud Message",
    "click_action": "https://mydomainproject.com"
  }

  }

After I POST the request I received the notification successfully in my background, and when I click on the notification it opens a new tab with the link in "click_action" that is already linked with my firebase service works. the problem here is when I try with another domain that has no relation with the domain that the service workers registered my token, it sends a notification but after clicking on it, it doesn't open a new tab with the custom URL.

What makes me asking myself is how other Push notifications platforms such as "PushAlert" have the access to send a custom URL like " https://google.com ", and in my PHP project, I can't receive the custom URL in the background.

About the foreground, I already know about how to add a listener in the javascript code, but this is not what I searching for. I want to know how can I send a custom URL like "Https://google.com", in the background using the firebase API.

this is my code:

           $msg = [
            'title' => 'test',
            'body' => 'Firebase Cloud Message',
            'click_action' => 'https://google.com'
            ];

          $payload = [
            'to' => $to,
            'priority' => 'high',
            'notification' => $msg
            ];

          $header = array(
              "authorization: key= barber",
              "content-type: application/json"
            );
          curl_setopt_array($curl, array(
            CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($payload),
            CURLOPT_HTTPHEADER => $header
          ));

Thanks for reading.




Aucun commentaire:

Enregistrer un commentaire