Firebase发送带有服务器API的密钥对值的通知



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

我没有得到的是

  1. 如何将数据包括在通知中?
  2. 如何在活动中接受他们?

我尝试过遵循指南,但我发现那些带有Firebase Console的数据的发送通知。

//对于服务器端

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

在您的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,为了在通知中添加数据,您应该像使用notification参数一样使用data参数。喜欢:

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

然后只需在您的$fields中添加:

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

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

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

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

有关更多详细信息,请参见Android中的处理消息。

相关内容

  • 没有找到相关文章

最新更新