![](/img/trans.png)
[英]Android unable to receive notification when app is killed using 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
。
我想刷新recyclerView
在fragmentNewJobs.java
只有fragmentNewJobs
是開別人收到通知。
recyclerView
添加新項(如果可能,不要加載所有數據) 我只想向回收者視圖添加新項(如果可能,不要加載所有數據)
我只想在fragmentNewJobs打開的情況下刷新fragmentNewJobs.java中的recyclerview,否則會收到通知。
為此,您可以在Application類中使用一個靜態布爾值,並在Fragment的onResume()
上將其設置為true,在onPause()
上將其設置為false。
然后可以通過ApplicationClass.isFragmentVisible()
檢查Fragment的可見性。
編輯
您可以在Fragment
setUserVisibleHint()
中更改fragmentVisible
布爾值,這將完美地工作。 您最初需要將UserVisibleHint
false,因為它是默認設置。 檢查此答案以獲得更好的理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.