簡體   English   中英

接收fcm通知時更新回收者視圖

[英]update recycler view while receive fcm notification

我想在接收FCM通知時(如果打開了特定的片段)刷新片段中的回收站視圖,否則接收通知。

我嘗試了多種解決方案,但是沒有用。

MyFirebaseMessagingService.java

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

    if (remoteMessage.getNotification() != null) {
        String noti_body = remoteMessage.getNotification().getBody();
        String noti_title = remoteMessage.getNotification().getTitle();
        String noti_activity = data.get("activity");

        mBuilder = new NotificationCompat.Builder(this, "M_CH_ID")
                .setSmallIcon(R.mipmap.logo)
                .setContentTitle(noti_title)
                .setContentText(noti_body)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo))
                .setColor(getResources().getColor(R.color.colorAccent))
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(NotificationManager.IMPORTANCE_HIGH)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setAutoCancel(true); // clear notification after click

    if (remoteMessage.getData().size() > 0) {
    fragmentNewJobs c = new fragmentNewJobs();
    if (c.getUserVisibleHint()) {//fragmentNewJobs.is_open
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("activity", noti_activity);
        getApplicationContext().startActivity(intent);
    }else {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("activity", noti_activity);

        @SuppressLint("WrongConstant")
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
        mBuilder.setContentIntent(pi);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
    }
   }
  }
}

fragmentNewJobs c =新的fragmentNewJobs(); 如果(c.getUserVisibleHint())

我已經嘗試過了,但是即使我打開了任何屏幕,它也會加載fragmentNewJobs.java

  1. 我想刷新recyclerViewfragmentNewJobs.java只有fragmentNewJobs是開別人收到通知。

    1. 我只想向recyclerView添加新項(如果可能,不要加載所有數據)

我只想向回收者視圖添加新項(如果可能,不要加載所有數據)

  • 您可以使用BroadcastManager將通知事件發送到現有的Activity。 當正在運行的活動接收到新的廣播時,它將在列表中添加新項目。
  • 通知Activity的另一個選項可以是EventBus ,它是快速增長的易於實現的庫,用於在服務,活動和片段之間進行通知。

如何使用BroadcastManager?

如何使用EventBus?

我只想在fragmentNewJobs打開的情況下刷新fragmentNewJobs.java中的recyclerview,否則會收到通知。

為此,您可以在Application類中使用一個靜態布爾值,並在Fragment的onResume()上將其設置為true,在onPause()上將其設置為false。

然后可以通過ApplicationClass.isFragmentVisible()檢查Fragment的可見性。

編輯

您可以在Fragment setUserVisibleHint()中更改fragmentVisible布爾值,這將完美地工作。 您最初需要將UserVisibleHint false,因為它是默認設置。 檢查此答案以獲得更好的理解。

您可以使用事件總線將消息從接收者發布到特定片段或活動。

並findout如果活動是可見或不可見看看這個

或者,如果您正在使用片段,則可以在Application類中使用靜態變量來跟蹤片段(最基本的用法)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM