Firebase使用来自服务器API的密钥对值发送通知

时间:2017-02-19 19:42:41

标签: android firebase android-notifications firebase-cloud-messaging firebase-notifications

我想通过服务器端的数据发送通知,并在我选择的活动中显示这些数据。到目前为止,我已设法从服务器发送通知并将其显示给我选择的任何活动。

我没有得到的是

  1. 如何在通知中包含数据?
  2. 如何在活动中接收它们?
  3. 我已尝试按照指南进行操作,但我只发现了那些来自Firebase控制台的数据发送通知。

    //用于服务器端

     <?php
      require "init.php";
       $message =$_POST['message'];
       $title=$_POST['title'];
     //firebase server  url
      $path_to_fcm='https://fcm.googleapis.com/fcm/send';
      // Here is the server_key
       $server_key="#########";
      $token="#########";
       $headers= array(
         'Authorization:key='.$server_key, 
         'Content-Type:application/json'
               );
                  // here use $tokens as the replace $key
    $fields= array('to'=>$token,
    'notification'=>array('title'=>$title,'body'=>$message,
      'click_action'=>'com.fredycom.tundevufirebasemysql_TARGET_NOTIFICATION'
    ));
       $payload= 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,$payload);
           $result =curl_exec($curl_session);     
           curl_close($curl_session); 
       mysqli_close($con);
        ?>
    

    //接收消息服务

    public void onMessageReceived(RemoteMessage remoteMessage) {
        // HANDLE THE FIREBASE NOTIFICATION DATA RECEIVED  WHEN APP RUN IN THE FOREGROUND
    
        if(remoteMessage.getData().size()>0){
            String title= remoteMessage.getData().get("title");
            String message= remoteMessage.getData().get("message");
            // now we send  the data to the  main activity
            Intent intent= new Intent("com.fredycom.myreartime_FCM_MESSAGE");
            intent.putExtra("title",title);
            intent.putExtra("message",message);
            LocalBroadcastManager localBroadcastManager= LocalBroadcastManager.getInstance(this);
            localBroadcastManager.sendBroadcast(intent);
        }
    

    //处理活动中的消息

       public class MainActivity extends AppCompatActivity {
       TextView title, message;
        @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
            // REGISTER BROADCAT RECEIVER
            LocalBroadcastManager.getInstance(this).registerReceiver(mHandler, new IntentFilter("com.fredycom.myreartime_FCM_MESSAGE"));
        setContentView(R.layout.activity_main);
        title = (TextView) findViewById(R.id.title_content);
        message = (TextView) findViewById(R.id.message_content);
    
        if (getIntent().getExtras() != null)
        {
            for (String key : getIntent().getExtras().keySet()) {
                if (key.equals("title"))
                {
                    title.setText(getIntent().getExtras().getString(key));
                }
                else if (key.equals("message"))
                {
                    message.setText(getIntent().getExtras().getString(key));
                 }
                }
               }
              }
          // HERE  IS  THE BROADICAST RECEIVER GET DATA FROM SMS RECIVING SERVICE
           private BroadcastReceiver mHandler= new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String Mytitle= intent.getStringExtra("title");
            title.setText(Mytitle);
            String Mymessage= intent.getStringExtra("message");
            message.setText(Mymessage);
            }
           };
    
         protected  void onPause(){
             super.onPause();
             LocalBroadcastManager.getInstance(this).unregisterReceiver(mHandler);
         }
     }
    

1 个答案:

答案 0 :(得分:2)

在您的PHP代码中,您有

$fields= array('to'=>$token,
'notification'=>array('title'=>$title,'body'=>$message,
  'click_action'=>'com.fredycom.tundevufirebasemysql_TARGET_NOTIFICATION'
));

这将产生如下的有效载荷:

{
  "to": "#######",
  "notification" : {
    "title": "title",
    "body" : "message",
    "click_action" : "com.fredycom.tundevufirebasemysql_TARGET_NOTIFICATION"
  }
}

您只发送notification - 仅有效负载。因此,要回答#1,为了在通知中添加数据,您应该像使用data参数一样使用notification参数。像这样:

$datamsg = array
(
    'key'   => 'value',
    'key2'  => 'value2',
    'key3'  => 'value3',
);

然后只需将其添加到$fields

$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $datamsg
);

对于#2,您已经有了接收data消息的代码,但如上所述,您发送的是notification - 仅有消息,因此无法< em>抓住任何东西。只需确保您指定了正确的密钥:

String value = remoteMessage.getData().get("key");
String value2 = remoteMessage.getData().get("key2");
String value3 = remoteMessage.getData().get("key3");

如果您只是希望从notification消息中获取详细信息,则应使用RemoteMessage.getNotification()。然后将数据发送到您的首选活动。只需确保您了解如何处理您发送的邮件,具体取决于有效负载。

有关详细信息,请参阅Handling Messages in Android