Determine if Activity is called by a Notification
Asked Answered
D

4

8

I am using an Activitiy with various Tabs on it. From a different part of the application, Notifications are created to tell the user that something has changed. I now managed to Call the Activity, when the user clicks on the Notification. But how can i determine wheter a Activity is created the "normal" way during runtime or by clicking on the notification?

(Depending on the notification clicked, i want to forward to another tab instead of showing the main Tab.)

Intent intent = new Intent(ctx, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0);

        // TODO: Replace with .Build() for api >= 16
        Notification noti = new Notification.Builder(ctx)
                .setContentTitle("Notification"
                .setContentText(this.getName())
                .setSmallIcon(R.drawable.icon)
                .setContentIntent(pendingIntent)
                .setDefaults(
                        Notification.DEFAULT_SOUND
                                | Notification.DEFAULT_LIGHTS)
                .setAutoCancel(true)
                .getNotification();

        NotificationManager notificationManager = (NotificationManager) ctx
                .getSystemService(Context.NOTIFICATION_SERVICE);

        // Hide the notification after its selected
        notificationManager.notify(this.getId(), noti); 

This successfully calls my MainActivity. But is there some Method that is called when the Activity is triggered by the pendingIntent?

Thought about to define something like this in the Main Activity:

onTriggeredByNotification(Notification noti){
     //determinte tab, depending on Notification.
}
Damalis answered 2/1, 2013 at 12:31 Comment(0)
G
21

Pass a boolean value from notification and check for the same in the onCreate method of the activity.

 Intent intent = new Intent(ctx, MainActivity.class);
 intent.putExtra("fromNotification", true);

...

if (getIntent().getExtras() != null) {
  Bundle b = getIntent().getExtras();
  boolean cameFromNotification = b.getBoolean("fromNotification");
}
Gothicize answered 2/1, 2013 at 12:45 Comment(0)
M
1

You can try this in your Notification

Intent intent=new Intent();
intent.setAction("Activity1");

In the Activity override onNewIntent() method and get action so you can determine the activity is called or not.

Mcgehee answered 2/1, 2013 at 12:37 Comment(2)
I tried that, but the onNewIntent Method is not triggered at any Time.Damalis
Ok then try to get the intent in onCreate() of activity.Mcgehee
H
1

Better than using the reserved action field of your intent as specified by @ricintech, you could use an extra parameter in your pending intent and detect it in your onCreate method and in your onNewIntent metod inside your activity.

Harman answered 2/1, 2013 at 12:39 Comment(0)
C
0

Add android:launchMode="singleTop" in manifest file ,then only onNewIntent() will get call

Collator answered 19/5, 2023 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.