androidnotificationscustom-url

How to detect if an app is opened from custom url or notification - android


I'm working on an app in android. It can be opened both from a custom URL and from a notification (when the user clicks on it). I've learned how to do both and know is working correctly. What I want to know is how I can detect when it's open from the URL and when from the notification (it has to be some difference when I handle it in the Main activity).

Thanks for the help!


Solution

  • It is pretty simple. Just set an extra boolean in the Intent and when activity is opened then check if it is opened from custom url or notification.

    Let's say you are setting intent for Notification as following, put an extra in intent:

    Intent intent = new Intent(context, MainActivity.class);
     intent.putExtra("IS_FROM_NOTIFICATION",true);
    

    In oncreate method of Activity check its value

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
         boolean isFromNotification = getIntent().getBooleanExtra("IS_FROM_NOTIFICATION",false);
    
    }