Skip to content

FCM sent to “/topics/all” is not received

I have tried sending the FCM notification using the code below

        json_data = {
            "to": msg_to,
            "notification": {
                "body": msg,
                "title" : title,
                "icon": icon,
                "click_action": url
            },
        }

        url = 'https://fcm.googleapis.com/fcm/send'
        myKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
        data = json.dumps(json_data)
        headers = {'Content-Type': 'application/json', 'Authorization': 'key=%s' % myKey}
        req = urllib2.Request(url, data, headers)
        f = urllib2.urlopen(req)

I am not receiving the notification in my browser when send the message “to”: /topics/all

However if I am sending the message with “to”: my registration token, I am receiving the notification.

Can some one tell me why this is happening. I understand that the message sent to /topics/all should reach all registered users and all is a default topic without any need to subscribe specifically.

Answer

You seem to have assumed that clients/tokens are automatically subscribed to /topics/all, which is no the case. There is no such thing as a default topic.

You’ll have to subscribe each client/token to the topic, which (in the case of a web client) means you’ll have to set up server-side code to do so securely as shown here.