Categories
android firebase firebase-cloud-messaging php

How can I send a Firebase Cloud Messaging notification without use the Firebase Console?

245

I’m starting with the new Google service for the notifications, Firebase Cloud Messaging.

Thanks to this code https://github.com/firebase/quickstart-android/tree/master/messaging I was able to send notifications from my Firebase User Console to my Android device.

Is there any API or way to send a notification without use the Firebase console? I mean, for example, a PHP API or something like that, to create notifications from my own server directly.

7

269

Firebase Cloud Messaging has a server-side APIs that you can call to send messages. See https://firebase.google.com/docs/cloud-messaging/server.

Sending a message can be as simple as using curl to call a HTTP end-point. See https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol

curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" \
    --Header "Content-Type: application/json" \
    https://fcm.googleapis.com/fcm/send \
    -d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"title\":\"Hello\",\"body\":\"Yellow\"}}"

You can all this REST API from within any environment, but there are dedicated so-called Admin SDKs for many platforms listed here.

20

  • 4

    How can I get the device id on iOS? Is it the device token we get on didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData or the long one we get with FIRInstanceID.instanceID().token() ?

    May 26, 2016 at 13:33

  • 4

    Frank I followed the guide at firebase docs and codelabs for adding push notifications on a progressive webapp and using POstman to post a http request, yet i keep getting 401 error. ANy suggestions. I’m copying my server key directly from my firebase console.

    – jasan

    May 26, 2016 at 19:58

  • 37

    how to send to all users rather than a particular users or topics?

    – vinbhai4u

    Aug 9, 2016 at 6:48

  • 3

    I got this error message back in one of my early tries with the CURL snippet: Field “priority” must be a JSON number: 10. After I removed the quotation marks from the 10 at the end, it worked.

    Sep 29, 2016 at 19:11


  • 3

    @vinbhai4u for send to all users, is needed to subscribe theme to ‘all’ topic. and so send to ‘/topics/all’

    Jan 19, 2020 at 10:04

67

This works using CURL

function sendGCM($message, $id) {


    $url="https://fcm.googleapis.com/fcm/send";

    $fields = array (
            'registration_ids' => array (
                    $id
            ),
            'data' => array (
                    "message" => $message
            )
    );
    $fields = json_encode ( $fields );

    $headers = array (
            'Authorization: key=' . "YOUR_KEY_HERE",
            'Content-Type: application/json'
    );

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    echo $result;
    curl_close ( $ch );
}

?>

$message is your message to send to the device

$id is the devices registration token

YOUR_KEY_HERE is your Server API Key (or Legacy Server API Key)

6

  • Firebase Console have no save push messaging data into fcm.googleapis.com/fcm/send?

    Sep 17, 2016 at 7:39

  • 1

    to send push notification to browser from where i can get device registration id ?

    Jun 4, 2019 at 3:08

  • this works perfectly but, because of the long text I am getting {"multicast_id":3694931298664346108,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MessageTooBig"}]}. What can be done to fix this?

    Oct 22, 2019 at 14:11

  • @AlishaLamichhane Is your message bigger than 4096 bytes? If not you can base64 encode your message (there might be something wrong with the text). If it is bigger than 4096 bytes… well, that’s the FCM limit.

    – Rik

    Oct 22, 2019 at 15:14

  • to replaces registration_ids

    – Yohanim

    Mar 25, 2021 at 8:35

60

Use a service api.

URL: https://fcm.googleapis.com/fcm/send

Method Type: POST

Headers:

Content-Type: application/json
Authorization: key=your api key

Body/Payload:

{
   "notification": {
      "title": "Your Title",
      "text": "Your Text",
      "click_action": "OPEN_ACTIVITY_1"
   },
   "data": {
      "<some_key>": "<some_value>"
   },
   "to": "<device_token>"
}

And with this in your app you can add below code in your activity to be called:

<intent-filter>
    <action android:name="OPEN_ACTIVITY_1" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Also check the answer on Firebase onMessageReceived not called when app in background

8

  • Ankit, I can send to specific device id. However, I can’t send to all. What I’ve to put "to" : "to_id(firebase refreshedToken)" instead of device id? "all" is not working at all. I’m using C# WebRequest to send notification. @AshikurRahman your suggestion also welcome. I’m struggling and searching since 3-4 days.

    Feb 13, 2017 at 17:04


  • 5

    Nevermind. I found the solution. to : “/topics/all” will send notifications to all devices or if you want to target only IOS replace all with ios and for android, replace with `android’. These are the default topics set. I guess.

    Feb 14, 2017 at 4:44

  • developine.com/…

    Nov 11, 2017 at 15:36

  • Read this blogpost for more details – > developine.com/…

    Nov 11, 2017 at 15:39

  • @Ankit, Hi, could you please specify how to get the target device’s id?

    – Arj 1411

    Jan 16, 2018 at 6:37