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.
Related
I'm showing a notification using FCM on my android app. Everything is working as expected except one issue.
When user clicks on the notification, I need to close all background and foreground activities of my app and open the intent specified in the notification.
How can I do it?
I'm using pending intent like this
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
YOu can use 2 flags to clear all stacks
intentToLaunch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intentToLaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Try this,
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);
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);
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.
Activity.getIntent returns the Intent that created the Activity.
Is there any way to get the Intent that resumes the Activity in the case where the Activity goes from onPause to onResume?
The solution is to override Activity.onNewIntent which is called before onResume.
I found out how to do it add this code:
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Now this should be like this:
notification = new Notification(R.drawable.icon,
"Notify", System.currentTimeMillis());
notification.setLatestEventInfo(this, "App name",
"App message", PendingIntent.getActivity(this,
0, new Intent(this, Main.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP),
PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags |= Notification.FLAG_ONGOING_EVENT;