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.
Related
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.
I have MyActivity and MyService (which plays music). I have also notification (in Notifications bar) but when I press it, MyActivity opens up in default state (play button is not pressed for e.g., although the music is still playing). This worked OK before I started to work with binding - I am concerned it has to do something with it.
What could be wrong?
EDIT:
This is my Noification inside my service:
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), SoundRelaxerActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification();
notification.tickerText = "Soundrelaxer: "+trackTitle;
notification.icon = R.drawable.play;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(getApplicationContext(), "SoundRelaxer","Playing: " + trackTitle, pi);
startForeground(1, notification);
Try adding this flag to the Intent you're using in your Notification:
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
That way a new instance of your Activity will not be created if it exists.
I am trying to launch an intent from a notification, but for a reason I don't know the extra is not always sent with the intent.
This is where I create the intent :
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
mContext.getString(R.string.text), 0);
Intent intents = new Intent(mContext, DataAppTabs.class);
intents.putExtra(DataAppTabs.REQUEST, DataAppTabs.REQUEST_DMTAB);
intents.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent launchIntent = PendingIntent.getActivity(mContext.getApplicationContext(), DataAppTabs.REQUEST_DMTAB, intents, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(mContext, "Info", mContext.getString(R.string.enginedmnotification_rebootneeded), launchIntent);
notification.flags = Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
notification.defaults = Notification.DEFAULT_ALL;
notificationManager.notify(DataAppTabs.REQUEST_DMTAB, notification);
In the DataAppTabs class I overrided onCreate() and onNewIntent(). When the notification appears, if I click on it it opens the correct Activity (DataAppTabs, which is a TabActivity), it triggers onCreate or onNewIntent if the activity was already started, but the extra "REQUEST" is not always set (getExtras() is sometimes null).
I have found the same question here with answers telling me I had to use the "FLAG_ACTIVITY_SINGLE_TOP" flag, but it still doesn't work all the time and I am not able to reproduce the problem every time.
Is it something I did wrong ?
Thanks
// Displaying Notification
NotificationManager manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "MyNotification", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), Sample.class), 0);
notification.setLatestEventInfo(getApplicationContext(), "Notification Title", "Notification Data", contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
int NOTIFICATION_ID=(int)System.currentTimeMillis();
manager.notify(NOTIFICATION_ID, notification);
// Sample.class is the activity to which you want to go when you click on Notification
I would like to create a notification that clears automatically when the user presses it and has no other behaviuour. The notification below works fine but when i press on it it takes me to the activity "MyActivity" (even having to define an intent seems a bit unecessary when I don't want to use it...)
Using FLAG_AUTO_CANCEL doesn't seem to have any effect at all.
Update: Sorry, I have found the FLAG_AUTO CANCEL does work, that is the notification is cleared from the status bar. I guess I am really tring to write an intent that does nothing (or completely delete the intent).
Code...
Notification notification = new Notification(R.drawable.success, res.getString(R.string.messages_sent), System.currentTimeMillis());
//Define the expanded message and intent
Context context = getApplicationContext();
CharSequence contentTitle = res.getString("My content title");
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0 );
notification.setLatestEventInfo(context, contentTitle, mStrSuccessMessage, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//Pass the notification to the notification manager
mNotificationManager.notify(1, notification);
Use an empty intent without an actual action.
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(), 0);
I don't think you can. You can have MyActivity exit immediately though: call finish() from onCreate().