0

I have this scenario when user clicks the notification then, if the app is opened/foreground I want to redirect the user to HomeActivity else redirect the user to SplashActivity as I am performing some authentication tasks there. So, what is the best and proper way to achieve this??

I know there are lot of related questions but I haven't found anything specific to my usecase

Debu
  • 615
  • 7
  • 21

2 Answers2

0

To redirect the user to a specific Activity based on your logic you can use PendingIntent.

To check a working example click here.

OR

try below code on button click.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.stackoverflow.com/"));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    builder.setContentIntent(pendingIntent);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    builder.setContentTitle("Notifications Title");
    builder.setContentText("Your notification content here.");
    builder.setSubText("Tap to view the website.");

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Will display the notification in the notification bar
    notificationManager.notify(1, builder.build());

To check if your app is in the foreground or not Check out this post.

nimi0112
  • 2,065
  • 1
  • 18
  • 32
  • checking the running process every time is what I want to avoid, looking for some cleaner solution – Debu Jul 31 '18 at 11:49
  • 1
    @Debu check this out https://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service – nimi0112 Jul 31 '18 at 11:51
0

After some searching in this forum I got this post , taking some guidance from that I figured out a solution, on notification click I am redirecting every intent to HomeActivity and in onCreate() put my code to check if authentication is done or not(before setContentView()). If the authentication is not done redirect to SplashActivity and finish the current activity, otherwise continue.

Debu
  • 615
  • 7
  • 21
  • yes but when my app is in background state, i'm not able to get the navigation preoperly wherever i want to be.can you please paste that code whichh is resolved your problem rather than a text explanation. – Dolly Rosy Mar 12 '19 at 11:01
  • And when app is in background, tap on notification it will take me to the default activity,but i want to go to another activity – Dolly Rosy Mar 12 '19 at 11:03
  • you just have to write your navigation logic in the default activity before setContentView(..) – Debu Mar 12 '19 at 13:47
  • can you please give me sample logic here, i'm not able to get the exact logic when app is in background. – Dolly Rosy Mar 12 '19 at 13:55
  • can you please paste that logic here , i'm not able t get the exact logic for that – Dolly Rosy Mar 13 '19 at 04:18
  • What is your exact usecase?? – Debu Mar 13 '19 at 07:30
  • https://stackoverflow.com/questions/55135422/fcm-tap-on-how-to-open-a-user-specified-activity-instead-of-default-activity-whe# === this is usecase – Dolly Rosy Mar 13 '19 at 07:40
  • Can you please tell me , how to resolved it and how to check the authentication is done or not ? – Dolly Rosy Mar 13 '19 at 11:48
  • in my case i am maintaining a token(firebase token) in shared preference, now when user comes to HomeActivity I am checking its validity, if it fails I am redirecting the user to splashactivity(via intent) else continue as usual – Debu Mar 13 '19 at 11:57
  • AAAAI9l2eEg:APA91bFC10ac_y_xE6d6ObTbwsWlJNDuSHj40gQlU0tBaYHVsRSfyynVevVBcYTY3szO6fmrBsxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -- you mean this type of firebase token are you using? – Dolly Rosy Mar 13 '19 at 12:32
  • Yes, Are you asking about how I am authentication users or how I am handling authentication on notification click?? – Debu Mar 13 '19 at 13:57
  • i'm asking about how to handle in Our launcher activity when tap on notification click i want to redirect to specific activity rathr than default activity – Dolly Rosy Mar 13 '19 at 14:04
  • right now when my app is in foreground tap on notificaion it will go to specified activity properly but when app is in background it is not goint to specified activity. – Dolly Rosy Mar 13 '19 at 14:05
  • if(condition) { //goto Activity 1 – Debu Mar 13 '19 at 14:06
  • what is that condition checks? – Dolly Rosy Mar 13 '19 at 14:08
  • //////////put this code in the start of activity's onCreate() if (check if the user authentication is not done) { Intent intent = new Intent(this, SplashActivity.class); intent.putExtras(getIntent().getExtras()); startActivity(intent); finish(); } setContentView(this, R.layout.activity_home); ........rest of the home activity code – Debu Mar 13 '19 at 14:14
  • Thanks But how can check that authentication is done or not case? – Dolly Rosy Mar 13 '19 at 15:14
  • firebase provides method for that where you can check the current user(for more custom approach you have to write your own logic) – Debu Mar 14 '19 at 07:09
  • https://stackoverflow.com/questions/55135422/fcm-tap-on-how-to-open-a-user-specified-activity-instead-of-default-activity-whe# can you please look at my question. – Dolly Rosy Mar 14 '19 at 07:16