I am currently facing the problem of setting pending action for two different activities to notification.
I have a ParentActivity and a ChildActivity. I want open ChildActivity on notification click if currently it is running or paused, otherwise start ParentActivity.
I tried this :
.........
Intent resultIntent = new Intent(this, ChildActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ParentActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
.............
Above is not working for me. Everytime ChildActivity is starting on notification click.
And also as Faruk answered, I dont want this. Creating a notification's pending intent by checking ChildActivity's current state will not work.
Suppose notification created when ChildActivity was running but after creating the notification, user killed the app. So after killing the app, If user will click on notification then ChildActivity will start. I don't want that. I want if ChildActivity is not running or paused then ParentActivity should be started.
How can I achieve this?
Please help.
While there may be several ways to achieve this, following is the one I can think of.
First, you should get whether ChildActivity is active or not, through this link
Check whether activity is active
Store this in some variable childActive, then you can initialize different notificationIntents checking the value without using task TaskStackBuilder.
For example;
Intent notificationIntent = null;
if(childActive)
notificationIntent = new Intent(context, ChildActivity.class);
else
notificationIntent = new Intent(context, ParentActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Have your Notification launch a simple dispatch Activity. This Activity does the following in onCreate():
super.onCreate(...);
if (ChildActivity.running) {
// ChildActivity is running, so redirect to it
Intent childIntent = new Intent(this, ChildActivity.class);
// Add necessary flags, maybe FLAG_ACTIVITY_CLEAR_TOP, it depends what the rest of your app looks like
childIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(childIntent);
} else {
// Child is not running, so redirect to parent
Intent parentIntent = new Intent(this, ParentIntent.class);
// Add necessary flags, maybe FLAG_ACTIVITY_CLEAR_TOP, it depends what the rest of your app looks like
parentIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(parentIntent);
}
finish();
In ChildActivity do this:
public static boolean running; // Set when this Activity is active
In ChildActivity.onCreate() add this:
running = true;
In ChildActivity.onDestroy() add this:
running = false;
Related
In my App, I have MainActivity with bottom navigation. When I receive a new notification, when I click on it (to open it), I don't want to recreate my MainActivity if it already exists. So I tried to add flags, but it doesn't work at all...
Could you help me guys?
Intent newsFeedDetailsActivityIntent = NewsFeedDetailsActivity.newInstance(mNewsFeedId);
Intent mainActivityIntent = MainActivity.newInstance(MainActivity.createBundle(navItem, currentMs));
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder builder = TaskStackBuilder.create(MyApplication.getInstance())
.addNextIntentWithParentStack(mainActivityIntent)
.addNextIntent(newsFeedDetailsActivityIntent);
PendingIntent pendingIntent = builder.getPendingIntent((int) System.currentTimeMillis(), PendingIntent.FLAG_UPDATE_CURRENT);
// Set pending intent to notification
notification.setContentIntent(pendingIntent);
I try to use local notification and it's actions. I want to create a notification and handle multiple action types. My notification asks a question to the user. There are two options, yes or no. My implementation is below:
Intent yesReceive = new Intent(this, this.getClass());
yesReceive.setAction("YES");
PendingIntent pendingIntent = PendingIntent.getActivity(this, CODE, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.icon1, "Yes", pendingIntent);
It recreates the activity. But old activity already alive. When I press back button, I can see it. How can I replace the new activity?
You can use "finish()" to close down the activity before you move onto the next one.
Here is a simple example:
startActivity(intent); <- here I am telling the program to start the desired actitvity
finish(); <- Here I am asking the program to close the current activity before I move onto the next one.
Intent yesReceive = new Intent(this, this.getClass());
yesReceive.setAction("YES");
PendingIntent pendingIntent = PendingIntent.getActivity(this, CODE, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.icon1, "Yes", pendingIntent);
finish();
I solve this problem with below line
android:launchMode="singleTop"
I am trying to start my activity from the notification and it is working but instead of starting the same activity, another (copy) of my activity gets launched when i exit from it I find another copy of the activity beneath it.
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
....
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("item", currentObj);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
....
b.setContentIntent(pIntent);
how can I fix this ??
you have to use TaskStackBuilder and add its parent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
and you have add it in the notification
stackBuilder.addParentStack(MainActivity.class);
What it does is simple it just says android about the parent of the present class you have opened! This should slove your problem. I hope this is helpful. Thankyou
You can call finish(); before b.setContentIntent(pIntent);.
There is a flag you can set that starts the same activity instead of a new one each time:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("item", currentObj);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
You can add this line to your Activity inside manifest to prevent from loading the same activity multiple times
android:launchMode = "singleInstance"
OR
Flag the intent before starting Activity like this
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
I build custom notification that contain button and i want to listin when user press on it.
The button should not open any activity but only logic staff like change song.
the Code:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.toptext, nowPlayingTitle);
//this not work
Intent intent = new Intent(this, receiver.class);
intent.putExtra("UNIQ", "1");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT)
contentView.setOnClickPendingIntent(R.id.imageButtonPlay,
pendingIntent);
notification.contentView = contentView;
// this is to return to my activity if click somwhere else in notification
Intent notificationIntent = new Intent(this, MYACTIVITY.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(NOTIFICATION_ID, notification);
I don't get the hang of the setOnClickPendingIntent what need to be in the second param?
How can i just call a function after user press on the button?
im probably missing something cause i dont understand the receiver side and what happend after user press
You are missing the fact that the button you created actually doesn't belong to your application. It is created in another context, in another process. There is no way it can call your function.
Instead, when the user taps the button, that pending intent is fired. You can catch it by your receiver (in your activity), check some parameters and do the action.
Or you can implement a service and handle this intent in background. I'd prefer this way.
thanks for quick answer. I try using receiver but it never fired.
The code is in the main question and i created for the reciever class the following code:
public class receiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
}
}
but click on the notification never fire the receiver ( Test on debug mode )
Wasn't really sure how to search for this...
I have a the following which is called whenever a job is added or removed from my queue to put a notification in the status bar:
private void showNotification()
{
int jobsize = mJobQueue.size();
int icon = (jobsize == 0) ?
android.R.drawable.stat_sys_upload_done :
android.R.drawable.stat_sys_upload;
Notification notification =
new Notification(icon, "Test", System.currentTimeMillis());
Intent intent = new Intent(this, FileManagerActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.flags =
(Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL);
notification.setLatestEventInfo(this,
"Uploading to our servers",
getString((jobsize > 0) ?
R.string.notification_active_transfers :
R.string.notification_no_transfers),
pendingIntent);
mNotifyManager.notify(NOTIFICATION, notification);
}
As it is now the behavior is this:
if the user logs out and hits the notification, it will still open a new FileManagerActivity (ops!) I could get around this by starting at my authentication activity and passing the intent up my stack in a natural order, its when the application is already running is where I have difficulties.
if the user already has the FileManagerActivity open clicking the notification will put a second instance over it. In this case, I want the currently running FileManagerActivity to recieve focus instead of launching a new instance.
How could I get the correct behavior?
I've done this before by setting my Activity to use the launch mode 'singleTop' in the Application Manifest. It will achieve the desired function, using the existing activity if one exists. In this case, onNewIntent will be called in your activity.
You'll need to check in your FileManagerActivity for authentication and start a new activity as appropriate if the user is not logged in.
I think Worked when added these:
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent.FLAG_UPDATE_CUR
Intent intent = new Intent(context, MyOwnActivity.class);
intent.putExtra("foo_bar_extra_key", "foo_bar_extra_value");
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT);