I want make a notification, that when clicked on it will bring my app from the background to the front. I am using the following code:
NotificationManager noma = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent pen = PendingIntent.getActivity(Timer.this, 0, intent, 0);
intent.putExtra("key", "trigerred");
String body = "This is a message";
String title = "This is title";
Notification no = new Notification(R.drawable.ic_launcher, body, System.currentTimeMillis());
no.defaults = Notification.DEFAULT_ALL;
no.setLatestEventInfo(this, title, body, pen);
noma.notify(uniqueID, no);
When I click on the notification that makes a new intent but I want the last created intent brought to the front. How i can do this?
You need to set the FLAG_ACTIVITY_SINGLE_TOP flag on the intent that you pass to getActivity.
This should bring you back to the all ready running activity when clicking your notification.
See here for a list of the different launch flags.
Try this
PendingIntent pen = PendingIntent.getActivity(Timer.this, 0, intent,Intent.FLAG_ACTIVITY_TASK_ON_HOME);
Related
I am creating a notification from a service; the notification is shown, but when I click on it, nothing happens: It was supposed to open an activity.
My code:
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "test", when);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, "title", "message", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
However if I use pretty much the same code from inside an activity, I can click on the notification, and my activity is shown. What am I doing wrong?
EDIT:
It turns out that there was nothing wrong with this code, there was a different issue:
When my service finished, it created the notification with the code above. However, the service also broadcasted that it was finished, and the receiver created another notification, which used a different code to create the notification (with no PendingIntents, so no defined action when the notification is clicked), and that notification must have placed itself instead of the original, correct one.
On top of using Notification.Builder for above Android 3.0, or NotificationCompat.Builder in support library v4 as #Raghunandan suggests in the comment, I had the same problem with a possible common solution to your problem.
This is specific to 4.4 as seen here:Issue 63236:Notification with TaskStackBuilder.getPendingIntent() is not open the Activity and here Issue 61850: KitKat notification action Pending Intent fails after application re-install
One confirmed solution is to perform cancel() operation on an identical PendingIntent with the one you are about to create.
What worked for me was to modify the target Activity's manifest definition and add
android:exported="true" within "activity" tags for the target Activity. That would be MainActivity in your case I assume.
Example:
<activity
android:name="com.your.MainActivity"
android:exported="true" >
.
.
</activity>
This works with api level 8.
private int NOTIFICATION_ID = 1;
private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mContentIntent;
private CharSequence mContentTitle;
you can create notification like this :
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//create the notification
int icon = R.drawable.ic_launcher;
CharSequence tickerText = mContext.getString(R.string.noti_comes); //Initial text that appears in the status bar
long when = System.currentTimeMillis();
mNotification = new Notification(icon, tickerText, when);
//create the content which is shown in the notification pulldown
mContentTitle = mContext.getString(R.string.noti_comes_t); //Full title of the notification in the pull down
CharSequence contentText = clck_see_noti; //Text of the notification in the pull down
//you can set your click event to go to activity
mContentIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, MainActivity.class), 0);
//add the additional content and intent to the notification
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
//make this notification appear in the 'Ongoing events' section
mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL ;
//show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
and do not forget the your service is registering in the manifest
<service
android:name="com.xx.your_service"
android:enabled="true"
>
</service>
If you define an Activity that is not registered into the AndroidManifest.xml the notification will not show any error message, and nothin happen.
Intent notificationIntent = new Intent(this, MainActivity.class);
be sure to have the Activity to start from the notification registered into the AndroidManifest.xml
This guy had a similar problem "I misspelled my activity name in the manifest.":
launch activity from service when notification is clicked
Im my case my project use Android Annotations. So my error was when I created the Intent, I always set this:
Intent notificationIntent = new Intent(this, MainActivity.class);
But is necessary to set the Android Annotation's generated class:
Intent notificationIntent = new Intent(this, MainActivity_.class);
That's solves my problem
I stuck in a problem.
Suppose there is an activity A
now, user press Home key, and put A into background.
now, after remaining in background for a while, it will generate a notification.
On clicking on that notification, it will bring back the exact same activity which was in the background.
Like, after pressing home key, if user start the application again by clicking on the icon or from the recent launched history, then android re-opens exactly last activity in it's previous state.
Here, I dont want to detect which was the last activity. My activity is fixed, I just want to bring it back at its previous state on clicking on the notification ?
Is that possible ? how to do that ?
here is my code for notification,
private void generateNotification(Context context, String message, String data) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification, message, System.currentTimeMillis());
Intent notificationIntent = new Intent(context, MenuActivity.class);
//notificationIntent.setFlags(Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
Bundle bundle = new Bundle();
bundle.putString("data",data);
notificationIntent.putExtras(bundle);
//Constants.NOTIFICATION_ID++;
notification.setLatestEventInfo(context, context.getString(R.string.app_name), message, PendingIntent.getActivity(context, 0, notificationIntent, Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(Constants.NOTIFICATION_ID, notification);
}
Use Intent.FLAG_ACTIVITY_CLEAR_TOP instead of FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY.
I raised a notification . What i want is , that when the user clicks on the notification , my activity is brought to the front without creating a new instance of it .
For this , I added the flag , REORDER_TO_FRONT , but still oncreate is being called instead of onNewIntent when i click on the notification .
This is my code -
int icon = R.drawable.android;
long when = System.currentTimeMillis();
CharSequence text = "new message";
CharSequence contentTitle = stanza.getFrom(); // message title
CharSequence contentText = stanza.getBody(); // message text
Intent notificationIntent = new Intent(context, ChatBox.class);
notificationIntent.putExtra("buddyid",stanza.getFrom());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, text, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,notification);
Have u tried:
Intent.FLAG_ACTIVITY_CLEAR_TOP
with your notificationIntent.addFlag();
The solution for me for this was to make a broadcast receiver that listens to broadcast actions that the notification triggers. So basically:
Notification triggers a broadcast action with an extra the name of the activity to launch.
Broadcast receiver catches this when the notification is clicked, then creates an intent to launch that activity using the FLAG_ACTIVITY_REORDER_TO_FRONT flag
Activity is brought to the top of activity stack, no duplicates.
I am using the following code to show a notification in my android application, but using this, with the notification an activity also shows up (i.e. a black blank screen) I dont want this screen, but just a simple notification and when the user clicks the notification then I want to launch the activity. What should be done to this code?
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(R.drawable.mcube_logo, "Message Sent", Calendar.getInstance().getTimeInMillis());
Intent intent = new Intent(RecieveAlarm.this, otherActivity.class);
PendingIntent pendingIntent =PendingIntent.getActivity(RecieveAlarm.this, 1, null, PendingIntent.FLAG_CANCEL_CURRENT);
notify.setLatestEventInfo(RecieveAlarm.this, "Title", "Details", pendingIntent);
notificationManager.notify(0, notify);
I suggest you to use:
Notification not = new Notification(idIcon, text, System.currentTimeMillis());
PendingIntent pInt = PendingIntent.getActivity(ctx, 0, new Intent(ctx, the_class_to_call), 0);
not.setLatestEventInfo(ctx, app_name, text, pInt);
and then not.notify....
use AlertDialog, then launch your desired activity on positive button click
I'm trying to get my Notification to not cancel when the user presses "Clear All" So far I have the intent working properly on everything except this:
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.addFlags(Notification.FLAG_ONGOING_EVENT);
intent.addFlags(Notification.FLAG_NO_CLEAR);
PendingIntent contentIntent = PendingIntent.getActivity(
getBaseContext(), 0, intent, 0);
The question I have at this point is: Are my flags correct?
Yes, your flags look pretty much correct, although I don't know if you even need the FLAG_NO_CLEAR. I currently have an app which creates an ongoing (non-cancellable) notification - I only use the FLAG_ONGOING_EVENT and it works fine for me. I pretty much just copied it from a tutorial and then added the ongoing event flag.
Here's some sample code:
String text = "notification";
Notification notification = new Notification(R.drawable.icon, text,
System.currentTimeMillis());
//launch the activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent().setComponent(ComponentName.unflattenFromString("com.example/com.example.MyActivity")), 0);
// set the label and text...
notification.setLatestEventInfo(this, getText(R.string.notification_label),
text, contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT;
// Send the notification.
// We use a string id because it is a unique number. We use it later to cancel.
NotificationManager mNM;
mNM.notify(R.string.notification_label, notification);