Firebase Notification - send to user segment vs se

2019-01-24 22:22发布

问题:

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 :)

回答1:

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)


回答2:

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.



回答3:

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.