andrewb
andrewb

Reputation: 3095

How to listen out for firebase notifications on any activity

I have followed a couple of tutorials to send notifications from firebase to an Android application, and I am able to receive the notifications no problem.

I am trying to understand 2 things:

  1. How do I handle the receiving of a notification while the user is viewing an activity which does not process the notification?

  2. When the app is running but is not in the foreground and I receive a notification, when I click it from the system tray the application opens up the activity which has the broadcaster defined - how do I control which activity is opened?

Here is the code from the activity which handles the notifications

public class DisplayNotificationActivity extends AppCompatActivity {
    private BroadcastReceiver mRegistrationBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_notification);

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // checking for type intent filter
            if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
                // gcm successfully registered
                // now subscribe to `global` topic to receive app wide notifications
                FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
            } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
                // new push notification is received
                String message = intent.getStringExtra("message");
                Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
                txtMessage.setText(message);
            }
        }
    };

If you need to see other parts of code let me know.

Upvotes: 2

Views: 1030

Answers (1)

Sharp Edge
Sharp Edge

Reputation: 4192

How do I handle the receiving of a notification while the user is viewing an activity which does not process the notification?

I will try to make it as short as possible. When your app is in foreground the notifications are received in onMessageReceived()method of your implementation of FirebaseMessagingService. Now which Activity should you broadcast the event to ? Make a Base Activity class and make all your Activities extend it. In the BaseActivity you can use the Broadcast approach or try using some Eventbus library like i do like this:

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onNotificationReceived(NotificationReceieveBean bean){

 if(bean != null){

       ViewUtils.showJobOfferSnackBar(this, bean.map);

       EventBus.getDefault().removeStickyEvent(bean);
    }



}

Any Activity extending the Base Activity will be able to receive the notification event. You can handle it accordingly.

When the app is running but is not in the foreground and I receive a notification, when I click it from the system tray the application opens up the activity which has the broadcaster defined - how do I control which activity is opened?

Usually when you click the system generated notification, your first activity opens up. You have to check the data in the Bundle instance has the same keys as the ones you're sending from the Server, in the onCreate() example:

    Bundle bundle = getIntent().getExtras();

    if(bundle != null){
        // if the below condition is true then it means you have received notification
        if(bundle.getString("notification_key_sent_from_server") != null) {
            ViewUtils.handleIncomingFCMNotification(bundle);

            finish(); // finishing the first activity since I want to go to some other activity
            return;

        }

    }

** EDIT **

public abstract class BaseNotificationHandlerActivity extends AppCompatActivity {

 protected int activityFlag = 0; // set a unique value from each extending Activity
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
}

 @Override
 public void onStart(){
  super.onStart();
  EventBus.getDefault().register(this);
}

  @Override
  public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
 }

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(NotificationItem notif) {
  if(activityFlag == 1){ 
   // this means Starting Activity, so handle for it particularly
       NotificationHelper notifHelper = new NotificationHelper(this);
       notifHelper.AddNotificationItem(notif);
   }
  else if(activityFlag ==2){
    // for some other Activity
    }

  }
}


public class StartingActivity extends BaseNotificationHandlerActivity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_starting);
  activityFlag = 1; // add unique flag for Activity here
}

 @Override
 public void onResume() {
 super.onResume();
}

Upvotes: 1

Related Questions