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.
Notification or data message can be sent to firebase base cloud messaging server using FCM HTTP v1 API endpoint. https://fcm.googleapis.com/v1/projects/zoftino-stores/messages:send.
You need to generate and download private key of service account using Firebase console and generate access key using google api client library. Use any http library to post message to above end point, below code shows posting message using OkHTTP. You can find complete server side and client side code at firebase cloud messaging and sending messages to multiple clients using fcm topic example
If a specific client message needs to sent, you need to get firebase registration key of the client, see sending client or device specific messages to FCM server example
Using Firebase Console you can send message to all users based on application package.But with CURL or PHP API its not possible.
Through API You can send notification to specific device ID or subscribed users to selected topic or subscribed topic users.
If you want to send push notifications from android check out my blog post
Send Push Notifications from 1 android phone to another with out server.
sending push notification is nothing but a post request to https://fcm.googleapis.com/fcm/send
code snippet using volley:
I suggest you all to check out my blog post for complete details.
Send messages to specific devices
To send messages to specific devices, set the to the registration token for the specific app instance
Send messages to topics
here the topic is : /topics/foo-bar
Send messages to device groups
Sending messages to a device group is very similar to sending messages to an individual device. Set the to parameter to the unique notification key for the device group
API URL :
https://fcm.googleapis.com/fcm/send
Headers
Request Method :
POST
Request Body
Messages to specific devices
Messages to topics
Messages to device groups
This works using CURL
$message
is your message to send to the device$id
is the devices registration tokenYOUR_KEY_HERE
is your Server API Key (or Legacy Server API Key)Use a service api.
URL:
https://fcm.googleapis.com/fcm/send
Method Type:
POST
Headers:
Body/Payload:
And with this in your app you can add below code in your activity to be called:
Also check the answer on Firebase onMessageReceived not called when app in background