1

I'm trying to add a new element to a listview when a notification is received(without users click).

I need to listen to received notification on my fragment, then add a new element to adapter based on notification info.

How can i listen to new notifications from a fragment?

I've already implemented the Firebase Cloud Messaging architecture.

-MyFirebaseMessagingService......

Can someone help me?

Hanzo
  • 1,839
  • 4
  • 30
  • 51

4 Answers4

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) {

            }
        };
Divyesh Boda
  • 258
  • 2
  • 12
0

you need to make one broadcast receiver in your fragment and when your firebase notification receive at a time you also call yor fragment receiver by below line

public String push_receiver = "com.example.fragment.GCMBroadCast";
Intent intent = new Intent(push_receiver);
                    intent.putExtra("extra", bundle);
                    sendBroadcast(intent);
Divyesh Boda
  • 258
  • 2
  • 12
  • What would be the intent filter when register the broadcastreceiver? LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourBroadcastReceiver, new IntentFilter(?)); – Hanzo Nov 02 '16 at 15:24
0

Declare the Arraylist and adapter as public in fragment and in MyFirebaseMessagingService update this method

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
    }
}
Arun Inbasekaran
  • 297
  • 1
  • 3
  • 14
0
  1. Create static method and call it from MyFirebaseMessagingService. reference1 , reference2
  2. Create BroadcastReceiver
  3. Use EventBus.
Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183