I am opening an activity on clicking notification, that is working fine. But if that activity (which I am opening on clicking notification) is already open (user has opened it) then I want to close it before opening it as a result of clicking notification. So how can I do that. Please help.
Code:
Intent intent = new Intent(context, ViewReminders.class);
intent.putExtra("CALLER","GenNot");
intent.putExtra("ID",notification_id);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
Try with setting the following flags
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
I am trying to open an activity when user clicks on Notification. I had issued following Intent as pending Intent
Intent caseIntent = new Intent(mContext, LoginActivity.class);
caseIntent.putExtra("priority", priority);
caseIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, caseIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT);
But when I click on the notification, if the app is not in forground the active which I have specified is not opening at all.
Is something wrong with this Intent?
from Android Development, i implement a simple notification by the sample code, but in my app, i don't want to new intent and create the activity again, i just want to back to my last activity(it's a mediaplayer UI). if i use the sample code, it will create a new activity by
Intent resultIntent = new Intent(this, ResultActivity.class);
i comment relative code about new intent and got a notification, but didn't have idea how to back to my last activity...
back to my app from long press home key and touch my app is OK.
what i want is just like this behavior.
bleow is the sample code from Android Development, i comment the new intent portion.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
/*Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);*/
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
[UPDATE]
i thought i need to set flag for intent and properity for activity.
therefore, for the activity i want to back to i set
android:launchMode="singleInstance"
and because, i don't want a new activity from intent, i just want my last activity, so i add
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
from documentation i got that using Pendingintent.contentIntent it must include the FLAG_ACTIVITY_NEW_TASK flag and When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. so i also add
Intent.FLAG_ACTIVITY_NEW_TASK
but from logCat, i still saw that when i touch the notification for back to activity, the pendingintent still called onCreate() function rather than just show the last activity which still "alive".
If you want to call the Activity from background try this:
Intent intent = new Intent(this, YourLauncherActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, 0);
mBuilder.setContentIntent(pendingIntent);
If you click the Notification while on Homescreen the last shown Activity of your App will be get to the foreground without starting it new. If the Activity was killed by the system you will get a new Activity.
I have used PendingIntent.getActivities instead of getActivity. This is working well in my project.
Intent backIntent = new Intent(this, HomeActivity.class);
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent notificationIntent = new Intent(this, NextActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivities(this, 1,
new Intent[] {backIntent, notificationIntent}, PendingIntent.FLAG_ONE_SHOT);
For me, this worked :
.setContentIntent(
PendingIntent.getActivity(
context,
0,
new Intent(context, YOUR_MAIN.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP ),PendingIntent.FLAG_ONE_SHOT))
FLAG_ONE_SHOT : DO NOT CREATE TWICE (associated with
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
In SingleTop mode, I use this method without define explicitly the root activity class:
Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)
.setComponent(getPackageManager().getLaunchIntentForPackage(getPackageName()).getComponent());
and this to resume last activity from notification:
builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0));
or this to resume last activity from a service:
try {
PendingIntent.getActivity(this, 0, intent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
}
Code hear
Intent resultIntent = new
Intent(getApplicationContext(),MainActivity.class);
resultIntent.putExtra("splash",2);
resultIntent.putExtra("message","test");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationCompatBuilder.setContentIntent(notifyPendingIntent);
Intent intent = this.getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
String extraValue = extras.getString("notificationType");
if(extraValue !=null){
int value = Integer.parseInt(extraValue);
if (value == 1) {
intent = new Intent(SplashActivity.this,MainActivity.class);
intent.putExtra("splash",value);
intent.putExtra("message", "fromsplash");
startActivity(intent);
finish();
} else
if (value == 2){
intent = new Intent(SplashActivity.this,MainActivity.class);
intent.putExtra("swip","2");
intent.putExtra("message", "test");
startActivity(intent);
finish();
}
}
}
}
I have an app which is counting down the time. I would like to get a notification in the notification bar while the time is up.
I've already done this:
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this).setTicker("Ticker Title").setContentTitle("Content Title").setContentText("Notification content.").setSmallIcon(R.drawable.iconnotif).setContentIntent(pIntent).getNotification();
noti.flags=Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(uniqueID, noti);
The problem occurs when I try to enter the app by clicking the notification.
When the notification is shown, I click it, but it doesn't do anything.
How to solve this problem?
Thanks for help! :)
Your Notification has a PendingIntent. This is used to define behaviour for clicking the notification. A pending intent has an intent as well, this intent contains info about application to launch, or in general, what to do after clicking the notification.
In your example, however, the intent that is included in your Pending Intent is:
// Empty intent, not doing anything
Intent intent = new Intent();
E.g. your intent does not define what to do. Change your intent to something like this:
// New Intent with ACTION_VIEW:
Intent intent = new Intent(Intent.ACTION_VIEW);
// Activity to launch
intent.setClassName("your.package.name", "ActivityToLaunch");
// Intent Flags
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
I have a setup where my activity starts a service and sends values through the intent that control how the service acts. These variables can change through the life of the service, and the service pushes updates back to the activity UI. If I exit the activity and relaunch it, it will request an update from the service to get the current values. This works fine if I re-launch the activity via the home screen, or if I use the recent apps menu. However if I launch the activity via the run in foreground ongoing notification I have set for the service, then the service re-sets itself to the original values when it was started.
Here's my run in foreground code, I don't think there is anything else that would have an affect on this problem.
//Create notification and set to run in forground
Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.app_name),
"Running Noise Based", pendingIntent);
startForeground(ONGOING_NOTIIFICATION, notification);
Update 3:
Adding the flags Intent.FLAG_ACTIVITY_NEW_TASK, Intent.FLAG_ACTIVITY_CLEAR_TOP, and Intent.FLAG_ACTIVITY_SINGLE_TOP to the notification intent fixed the problem. I have no idea why these fixed it. If anyone can tell me why this happens please do. Here's the new code that works.
//Create notification and set to run in forground
Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.app_name),
"Running Noise Based", pendingIntent);
startForeground(ONGOING_NOTIIFICATION, notification);