【问题标题】:Firebase Notification ListviewFirebase 通知列表视图
【发布时间】:2017-06-26 09:23:54
【问题描述】:

我已经使用我自己的服务器成功地将 Firebase 通知发送到 android 应用程序,并且我在单击通知的新活动中的列表视图中列出了消息。我的问题是如果我点击一个特定的通知,所有未读的通知消息都会显示在列表视图中

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,它会移动到带有列表视图的下一个活动,但两条消息都显示为“1. Mom 2. Dad”

【问题讨论】:

  • 第一种方法应该叫showNotification还是sendPushNotication?

标签: java android json listview firebase-cloud-messaging


【解决方案1】:

list.add(message); 不行,因为如果应用程序被系统关闭(因为用户打开另一个需要更多内存的应用程序),您会丢失数据。

您想要做的是向“意图”添加更多信息,例如

intent.setExtra(KEY, VALUE);

在活动中,您必须检索用于启动它的意图

getIntent()

【讨论】:

    【解决方案2】:

    您必须通过意图发送相关数据。

    intent.setExtra(KEY, VALUE);
    

    那么你必须使用getIntent()函数调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      相关资源
      最近更新 更多