phpandroidfirebasefirebase-cloud-messaging

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


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.


Solution

  • Update: the API called here stopped working in the second half of 2024. For an example of how to send a message with a REST request in the v1 API, see Send messages to specific devices

    From there:

    curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
    "message":{
       "notification":{
         "title":"FCM Message",
         "body":"This is an FCM Message"
       },
       "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
    }}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send
    

    You'll need to get an authorization token to be able to make this call. The process for that is documented in authorizing send requests.


    Note: the below answer no longer works as the endpoint/API it uses has been decommissioned. It is merely left for historical completeness. Use the approach above to send FCM messages with CURL from now on.

    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.