0

我想從服務器端發送數據通知並在我選擇的活動中顯示這些數據。到目前爲止,我已經設法從服務器發送通知並將它們顯示給我選擇的任何活動。Firebase發送來自服務器API的密鑰對值的通知

我不明白的是

  1. 如何包括通知數據?
  2. 如何在活動中接受他們?

我試過如此遵循指南,但我只發現那些用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 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

在你的PHP代碼//收到消息服務

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); 
    } 

//處理消息,你有

$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 -only有效載荷。因此,要回答#1,爲了在通知中添加數據,您應該簡單地使用data參數,就像使用參數notification一樣。像這樣:

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

然後,只需添加在你的$fields

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

#2,你已經擁有的代碼接收data消息,但正如上面提到的,你送notification - 只有留言,所以無法抓什麼。只要確保你指定正確的密鑰:

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瞭解更多詳情。

+0

謝謝sooo much @AL爲了拯救我的生活與這個令人難以置信的解釋..保持它... –

+0

不客氣。祝你的程序好運。 :) –

相關問題