I am using Intent to relaunch app from Notification Panel, but before to launch new instance how can close existing same open app, no matter in which state application is.
Any idea/suggestion.
Just add Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK when you create the Intent to launch your app.
NOTE: However, this will only work if your root Activity is still present in the task (ie: you didn't call finish() on it when starting any other Activity in your task.
Use have to use some flag, this might help u
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Also check your manifest for the
android:launchMode="singleTop"
attribute on the activity which launches from the notification.
Related
I've got a simple use case. I have activity open like below:
A > B > C
Now, I get a push notification.
I do the following in my GCMIntentService class:
In onMessage,
Intent notificationIntent = new Intent(context, A.class);
PendingIntent contentIntent =
PendingIntent.getActivity(context,0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(contentIntent);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
My expectation is that activities B, C should be removed from Activity stack and you should be re-directed to activity A.
What is happening is, however, A gets opened; but when you hit back button I get C, B, A again in that order!!
What is it that I'm doing wrong?
Here are the things I tried:
I've experimented with
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);,
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP, Intent.FLAG_ACTIVITY_SINGLE_TOP);
I've kept a static reference to the last foreground activity and passed that as context.
Nothing seems to work.
Try this. It may help you. Let me know once you have tested it.
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(notificationIntent);
try this Tutorial
Intent notificationIntent = new Intent(context, YourActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Set launch mode of your activity to "singleTask":
<activity
android:name=".ActivityA"
android:launchMode="singleTask"
>
And set flags as below:
Intent intent= new Intent(currentActivity, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
I want to launch an Activity(Afresh, Without any previously retained state) on a notification click. IF this Activity is already in the stack, bring this to top but without any previous state.
So Far i have tried setting Flags to Passed Intent.
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|INTENT.FLAG_ACTIVITY_SINGLE_TOP)
But They Don't serve the purpose. Any suggestions how i could achieve this.
If you remove |Intent.FLAG_ACTIVITY_SINGLE_TOP from your launch this should restart the activity.
Specifying |Intent.FLAG_ACTIVITY_SINGLE_TOP in the launch flags causes the existing instance to be reused. If you remove it the existing instance will be finished and a new instance will be launched.
Here You Go.
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
to open application in anystate.
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);
In my app I am sending GCM notification to device, when we click on notification it is not opening app in Samsung S4. Please any one help me.
Code:
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, Main.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
String title = context.getString(R.string.app_name);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0111, notification);
Add android:exported="true" in the AndroidManifest for the Activity specified by the intent.
This problem is dependent of Android Version not Samsung.......
Add android:exported = "true" in the manifest
<activity
android:name=".G_chat"
android:label="G_chat"
android:exported="true">
</activity>
G_chat is the activity which will be opened on the notification click.
I have a Android app with Google Cloud Message and Phonegap. Send, receive are done.
I try to launch my app from notification bar. But have problem:
This isn't the root activity. Clearing it and returning to the root activity.
How to fix it?
PendingIntent when create notification:
Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("pushBundle", extras);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Force main activity load
PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());
startActivity(launchIntent);
When i click to notification, it run Intent PushHandlerActivity, and PushHandlerActivity force main activity load. When the main activity load it say "This isn't the root activity. Clearing it and returning to the root activity." in LogCat, and my app can't show up :(
Please look for my code. It works absolutely fine for me
Intent notificationIntent = new Intent(context, SplashScreen.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Did you try to change:
Intent launchIntent = pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());
With
Intent launchIntent = pm.getLaunchIntentForPackage(<PackageNameOfYourMainActivity>);
Than it will start your PhoneGap main activity.
I didn't try this, it's just looks logical to me.
I am a bit lost with PendingIntent.
As far as I could understand, it's a Token given to the OS to perform later (hence pending) operations.
I have an activity that launched a service. The service, occasionally creates a notification.
What I am trying to do, as the simplest of all, is to bring the activity to the front.
I am not sure where and how I create and to whom I send the PendingActivity.
If I create it within the Activity, I need to send it to the service - HOW?
If I create it within the service, how would the context be to call the activity? are these the same? - I though these are the same, as how the OS works, but it did not work for me.
Here are some code lines
This is not working btw StartService gets an Intent.
This code is in my activity
Intent intent = new Intent(this, NeglectedService.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this,
0,
intent,
PendingIntent.FLAG_ONE_SHOT);
startService(contentIntent);
So, the correct one is
Intent intent = new Intent(this, NeglectedService.class);
startService(contentIntent);
So I think to make the pending intent in my service, but this didn't work for me, as I am not sure how to reuse/use the intent
Notification notification = new Notification(R.drawable.icon,
extra,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this,
0,
intent, // not sure what intent to use here !!!!
PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.FLAG_INSISTENT;
mNotificationManager.notify(id, notification);
solved
What needed to be done, is use the Neglected.class in the intent.