我正在尝试在收到通知时向列表视图添加新元素(没有用户点击)。
我需要收听我的片段收到的通知,然后根据通知信息向适配器添加新元素。
如何从片段中收听新通知?
我已经实施了Firebase云消息传递架构。
-MyFirebaseMessagingService ......
有人可以帮助我吗?
答案 0 :(得分:2)
public String push_receiver = "com.example.fragment.GCMBroadCast";
@Override
public void onResume() {
super.onResume();
mActivity.registerReceiver(broadcastReceiver, new IntentFilter(push_receiver_expositor));
}
/** receiving GCM broadcast */
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
答案 1 :(得分:0)
你需要在你的片段中制作一个广播接收器,当你的firebase通知一次收到时,你也可以通过下面的行调用你的片段接收器
public String push_receiver = "com.example.fragment.GCMBroadCast";
Intent intent = new Intent(push_receiver);
intent.putExtra("extra", bundle);
sendBroadcast(intent);
答案 2 :(得分:0)
在片段中将Arraylist和adapter声明为public,并在MyFirebaseMessagingService中更新此方法
private void handleNotification(final String message) {
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
//add message to list
Fragment.list.add(message);
Fragment.adapter.notifyDataSetChanged();
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message); LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
} else {
// If the app is in background, firebase itself handles the notification
}
}
答案 3 :(得分:0)
MyFirebaseMessagingService
调用它。 reference1,reference2