4

I am assigned a task to use Firebase to implement push notifications but I am kind of new this.

Looking at the documentation:

https://firebase.google.com/docs/notifications/android/console-audience

I can't tell under what circumstances should I use send to user segment or send to a topic.

Can someone please give me some example when to use one or the other and point out the difference? Thanks ahead :)

adjuremods
  • 2,766
  • 2
  • 10
  • 16
Tom
  • 14,222
  • 12
  • 61
  • 105

3 Answers3

6

User Segments

  • You can only send notifications to User Segments via the Firebase Console. (see here).
  • Limited to specific targets (from the docs you linked):

    Select the message target. The dialog displays further options to refine the target based on whether you choose App/App Version, Device Language, or Users in Audience.

  • As also already mentioned in the doc you linked:

    You can target predefined user segments or custom audiences created in Firebase Analytics.

Topics

  • Token/device management not necessarily required.
  • Unlimited number of subscribers.
  • Can send to topics using the FCM API.
  • Can easily subscribe/unsubscribe via the client app.

IMHO, if you want things to be quick and simple, go with Topic Messaging.

Community
  • 1
  • 1
AL.
  • 33,241
  • 9
  • 119
  • 257
  • I always use topic messaging because it saves a lot of time. As soon as user signs in I subscribe the user to his user id and whenever I have to send a push notification to that user from server I just post message his user id. I hope that is perfect rt? – Praveena Jan 07 '19 at 17:19
  • @Praveena Topics usage is a matter of preference. I don't see any issue for your use case. So I think it's okay. Just note that topics are *public* and that any other tokens may subscribe to it. – AL. Jan 08 '19 at 07:33
  • Actually this is `topics are public and that any other tokens may subscribe to it.` very important and I missed it. Thanks a lot – Praveena Jan 08 '19 at 08:39
5

Use User segements

  • To typically send push notifications to a specific and limited set of devices.
  • Message delivery is almost instantaneous (in my experience). Also, I haven't observed throttling as was the case earlier with GCM.

Use Topics

  • Topic or publish/subscribe mechanism is used for a comparatively larger audience and the information type is public. Examples are weather and news.
  • Topics have latency (Message delivery may be throttled)
Pravin Sonawane
  • 1,483
  • 1
  • 15
  • 31
0

first you have to save the tokens for every single device you want to send notification to, I've saved them on a table call "FCM_TOKEN", then retrieve tokens (I'm using PDO) and send them using while loop like this:

while($row=$statement->fetch(PDO::FETCH_BOTH))
            {
                $key = $row['Fcm_Token'];
                $headers = array(
                'Authorization:key=' .$server_key,
                'Content-Type:application/json');
                $fields = array('to'=>$key,
                    'notification'=>array('title'=>$titulo, 'body'=>$mensaje,
                        'click_action'=>'com.example.witch.gtslsac_app_1_TARGET_NOTIFICATION'
                        )); 
                        $playload=json_encode($fields);
            $curl_session = curl_init();
            curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
            curl_setopt($curl_session, CURLOPT_POST, true);
            curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
            curl_setopt($curl_session, CURLOPT_POSTFIELDS, $playload);

            $result = curl_exec($curl_session);
            echo $result;   
            }   

Don't forget to close session curl_close($curl_session); this worked just fine for me.