2017-05-14 49 views
1

我有一個接收FCM通知的應用程序,我想在另一個活動中顯示通知和數據有效載荷的內容,我的挑戰是當我點擊通知時,它指示我慾望的活動,但我沒有得到顯示的信息。FCM如何顯示通知中的數據點擊另一個活動

請幫幫忙,下面是我的代碼

MyFirebaseMessagingService 
public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Map<String, String> data = remoteMessage.getData(); 

     //you can get your text message here. 
     if (remoteMessage.getData().size() > 0) { 
      Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); 

      try { 
       String storedData = remoteMessage.getData().toString(); 
       // JSONObject json = new JSONObject(storedData); 

       JSONArray arr = new JSONArray(storedData); 
       JSONObject jObj = arr.getJSONObject(0); 
       String title = jObj.getString("title"); 
       String message = jObj.getString("body"); 
       sendPushNotification(jObj); 

       final String MY_PREFS_NAME = "MyFCMFile"; 
       SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
       editor.putString("Title", title); 
       editor.putString("Message", message); 
       editor.apply(); 

      } catch (Exception e) { 
       Log.e(TAG, "Exception: " + e.getMessage()); 
      } 
     } 
     else { 

      String title = remoteMessage.getNotification().getTitle(); 
      String message = remoteMessage.getNotification().getBody(); 
      String click_action = remoteMessage.getNotification().getClickAction(); 
      Intent intent = new Intent(click_action); 
      intent.addFlags(intent.FLAG_ACTIVITY_CLEAR_TOP); 
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); 
      notificationBuilder.setContentTitle(title); 
      notificationBuilder.setContentText(message); 
      notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); 
      notificationBuilder.setAutoCancel(true); 
      notificationBuilder.setContentIntent(pendingIntent); 

      final String MY_PREFS_NAME = "MyFCMFile"; 
      SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
      editor.putString("Title", title); 
      editor.putString("Message", message); 
      editor.apply(); 

      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(0, notificationBuilder.build()); 


     } 
    } 

    //this method will display the notification 
    //We are passing the JSONObject that is received from 
    //firebase cloud messaging 
    private void sendPushNotification(JSONObject json) { 
     //optionally we can display the json into log 
     Log.e(TAG, "Notification JSON " + json.toString()); 
     try { 
      //getting the json data 
      JSONObject data = json.getJSONObject("data"); 

      //parsing json data 
      String title = data.getString("title"); 
      String message = data.getString("message"); 
      String imageUrl = data.getString("image"); 

      final String MY_PREFS_NAME = "MyFCMFile"; 

      SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
      editor.putString("Title", title); 
      editor.putString("Message", message); 
      editor.apply(); 

      //creating MyNotificationManager object 
      MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext()); 
      // CREATE CLICK ACTION 

      //creating an intent for the notification 

      Intent intent = new Intent(getApplicationContext(), Notifications.class); 

      //if there is no image 
      if (imageUrl.equals("null")) { 
       //displaying small notification 
       mNotificationManager.showSmallNotification(title, message, intent); 
      } else { 
       //if there is an image 
       //displaying a big notification 
       mNotificationManager.showBigNotification(title, message, imageUrl, intent); 
      } 
     } catch (JSONException e) { 
      Log.e(TAG, "Json Exception: " + e.getMessage()); 
     } catch (Exception e) { 
      Log.e(TAG, "Exception: " + e.getMessage()); 
     } 
    } 
} 

我的願望activityto顯示通知和數據有效載荷信息-activity_notifications.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.Notifications"> 

    <FrameLayout 
     android:layout_width="368dp" 
     android:layout_height="551dp" 
     android:orientation="vertical" 
     > 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/job_notificatication" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="120dp" 
      android:orientation="vertical"> 

      <LinearLayout 

       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:background="@drawable/my_border" 
       android:orientation="vertical" 
       android:padding="10dp"> 

       <TextView 

        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="JOB INFO" 
        android:textStyle="bold" /> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 

         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Job Category :" 
         android:textStyle="bold" /> 


        <TextView 
         android:id="@+id/category" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="" 
         android:textSize="36sp" /> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 

         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Job Type:" 
         android:textStyle="bold" /> 


        <TextView 
         android:id="@+id/type" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="" 
         android:textSize="36sp" /> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 

         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="Job Details:" 
         android:textStyle="bold" /> 


        <TextView 
         android:id="@+id/details" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="" 
         android:textSize="36sp" /> 
       </LinearLayout> 
      </LinearLayout> 

      <LinearLayout 

       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:layout_marginTop="20dp" 
       android:background="@drawable/my_border" 
       android:orientation="vertical" 
       android:padding="10dp"> 

       <TextView 

        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="CUSTOMER INFO" 
        android:textStyle="bold" /> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 

         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="Name :" 
         android:textStyle="bold" 
        /> 
        <TextView 
         android:id="@+id/name" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="" 
         android:textSize="36sp" /> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 

         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="Address:" 
         android:textStyle="bold" /> 


        <TextView 
         android:id="@+id/address" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="" 
         android:textSize="36sp" /> 
       </LinearLayout> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 

         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="Landmark" 
         android:textStyle="bold" /> 


        <TextView 
         android:id="@+id/landmark" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="" 
         android:textSize="36sp" /> 
       </LinearLayout> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 

         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="Contact Person:" 
         android:textStyle="bold" /> 


        <TextView 
         android:id="@+id/contact_person" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="" 
         android:textSize="36sp" /> 
       </LinearLayout> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 

         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="Expected Date" 
         android:textStyle="bold" /> 


        <TextView 
         android:id="@+id/date" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="" 
         android:textSize="36sp" /> 
       </LinearLayout> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 

        <TextView 

         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="Expected Time:" 
         android:textStyle="bold" /> 


        <TextView 
         android:id="@+id/time" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="" 
         android:textSize="36sp" /> 
       </LinearLayout> 

      </LinearLayout> 
      <Button 
       android:id="@+id/accept" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="ACCEPT"/> 
     </LinearLayout> 
    </FrameLayout> 

</android.support.constraint.ConstraintLayout> 

的Notifications.java

package com.example.workman; 

import android.content.SharedPreferences; 
import android.support.annotation.IdRes; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

public class Notifications extends AppCompatActivity { 

    TextView title, message; 

    String txtTitle, txtMessage; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_notifications); 

     title = (TextView)findViewById(R.id.category); 
     message = (TextView)findViewById(R.id.type); 




    final String MY_PREFS_NAME = "MyFCMFile"; 
    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
    String txtTitle = prefs.getString("Title", "");//"No name defined" is the default value. 
    String txtMessage = prefs.getString("Message", "");//"No name defined" is the default value. 

     title.setText(txtTitle); 
     message.setText(txtMessage); 
} 
} 

回答

1

根據FCM文檔,看起來你做錯了。

onMessageReceived()只有在應用程序已經在前臺運行時纔會被調用。否則,Android將顯示通知(使用「通知」字典的屬性),並使用getClickAction()值打開所需的Activity,而無需編寫任何代碼。

而且你可以通過你的啓動器活動的意圖的額外獲得「數據」有效載荷。

即在Notifications類:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Bundle extras = getIntent().getExtras(); // you should find the data here 
    ... 
} 

下面是我工作:

在我的第二個Activity類(點擊通知時應當打開的一個),我把:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_display_message); 

    TextView text = (TextView) findViewById(R.id.data2); 
    if (text != null) { 
     Bundle extras = getIntent().getExtras(); 
     StringBuilder keys = new StringBuilder(); 
     if (extras != null) { 
      for (String key : extras.keySet()) 
       keys.append(key + " = " + extras.getString(key) + "\n "); 
     } 
     text.setText("extras on second activity: " + keys.toString()); 
    } 
} 

我得到了以下輸出:

extras on second activity: google.sent_time = null 
msg - some value (this is a key/value pair I included in the "data" dictionary) 
uri - some value (this is a key/value pair I included in the "data" dictionary) 
from - <the sender id> 
google.message_id - <the message id returned in the receipt of the HTTP response> 
collapse_key - com.package.fcmdemo (default value) 

正如您所看到的,我的(自定義)「數據」字典的屬性全部傳遞給活動。 「通知」字典的屬性沒有通過,但我假設這是故意的(因爲它們已經用於顯示我點擊的通知來打開活動)。

+0

沒有顯示,我得到了相同的結果,都一樣謝謝 – LaideLawal

+0

@LaideLawal請參閱編輯 – Eran

+0

如何獲取鍵值對中的每個值? – LaideLawal

相關問題