How to send fcm notification to all devices using http?

Viewed 633

I want to send notification to all devices, how can I do it with fcm http.

The endpoint I used: https://fcm.googleapis.com/fcm/send

Here is what I've tried.

{
    notification: {
        title: "Daily motivation!",
        body: "The greatest glory in living lies not in never falling, but in rising every time we fall. - Nelson Mandela",
        tag: "DAILY_QUOTE"
    },
    collapse_key: "dailyquote"
};

I have also tried with condition:

{
    notification: {
        title: "Daily motivation!",
        body: "The greatest glory in living lies not in never falling, but in rising every time we fall. - Nelson Mandela",
        tag: "DAILY_QUOTE"
    },
    condition: "!('nothingreally' in topics)"
    collapse_key: "dailyquote"
};

Headers:

{
    "Content-Type": "application/json",
    "Authorization": "key=" + myApiKey
}

Although, it works well when I send to specific registration IDs, but it doesn't work when I want to send to all devices. Am I missing something?

2 Answers

You can follow the below approach.

  • Get FCM token for device.

  • Register (Subscribe) FCM token under a topic (eg - daily-motivational-quotes).

  • Send notification to the topic (eg - daily-motivational-quotes)

https://fcm.googleapis.com/v1/projects/mobiquity-fcm/messages:send

{
  "message": {
    "topic": "daily-motivational-quotes",
    "notification": {
       "title": "Daily motivation!",
       "body": "The greatest glory in living lies not in never falling, but in rising every time we fall. - Nelson Mandela",
       }
}

You can register multiple devices under one topic.

https://fcm.googleapis.com//v1/projects/<YOUR-PROJECT-ID>/messages:send
Content-Type: application/json
Authorization: bearer <YOUR-ACCESS-TOKEN>

{
  "message": {
    "topic": "matc`enter code here`hday",
    "notification": {
      "title": "Background Message Title",
      "body": "Background message body"
    },
    "webpush": {
      "fcm_options": {
        "link": "https://dummypage.com"
      }
    }
  }
}
Related