简体   繁体   中英

firebase - php - send notification to everyone

I want to send a background notification from fcm to all my users. This is my code, the only problem I've faced is that I have to put a token id. I need to send notification to all of my users without defining the token values

This is my code :

    <?php
define('API_ACCESS_KEY','Api');
 $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
 $token='token';

     $notification = [
            'title' =>'title',
            'body' => 'body of message.',
            'icon' =>'myIcon', 
            'sound' => 'mySound'
        ];
        $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

        $fcmNotification = [
            //'registration_ids' => $tokenList, //multple token array
            'to'        => $token, //single token
            'notification' => $notification,
            'data' => $extraNotificationData
        ];

        $headers = [
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        ];


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$fcmUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
        $result = curl_exec($ch);
        curl_close($ch);


        echo $result;
?>

It works fine and send notifications successfully, but how can I send notification to everyone?

It looks like there is no such feature to send notifications to all devices.

However, there is a workaround for your problem using topics. For this you will need to subscribe all users to a specific topic during the app startup.

FirebaseMessaging.getInstance().subscribeToTopic("your_topic");

Then you can send the notification to that topic so that the notification will be sent to all users.

'to' => '/topics/your_topic', // using topic instead of token

If i get your question.

You need to subscribe a Topic and then you just need to use that Topic. The users who subscribed to it, will get the notification.

Docs:-

Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.

https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

There are two ways to send notification to multiple users.

  1. Create a topic with all the users in it and send the push to that topic. Example: https://firebase.google.com/docs/cloud-messaging/android/send-multiple

  2. Send notification to application the package id. But this will send notification even to the non authenticated user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM