Android - 在Listview中显示FCM通知消息点击通知

时间:2017-06-30 06:00:28

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

我已使用自己的服务器成功将Firebase通知发送到Android应用,我通过点击通知在新活动中列出ListView中的消息。我的问题是如果我点击一个特定的通知,所有未读的通知消息都会显示在ListView中。

FirebaseMessageservice.java:

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");
        list.add(message);
        Collections.reverse(list);
        System.out.println(list);

        MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());

        Intent intent = new Intent(getApplicationContext(), HomeActivity.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());
    }
}
  

HomeActivity.java

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ListView listView = (ListView) findViewById(R.id.notification_list);
        adapter = new ListViewAdapter(this, list);
        listView.setAdapter(adapter);

    }
}

输出屏幕

如果我点击Hello Mom,它会移动到listview的下一个活动,但两个消息都会显示在" 1。妈妈2.爸爸"

enter image description here

enter image description here

0 个答案:

没有答案