I have tested the new BigTextStyle Notification which display correctly. However when I add action to the notification, it never get cancel and the pull-down notification area is never close when click on the action. What did I do wrong?
Drawable d = getResources().getDrawable(android.R.drawable.ic_menu_add);
Bitmap b = ((BitmapDrawable)d).getBitmap();
Notification noti = null;
Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
String s = "Name: John \n"
+ "Surname: Doe\n"
+ "Vehicle: Honda Jazz TA-1234\n"
+ "Note: Good drifter";
Builder builder = new Notification.Builder(MainActivity.this)
.setContentTitle("New challenger arrived!")
.setContentText(s)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(b)
.setTicker("New challenger arrived!")
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setNumber(5)
.addAction(
android.R.drawable.ic_menu_add,
"Accept",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0, null))
.addAction(
android.R.drawable.ic_menu_delete,
"Refuse",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0, null))
.addAction(
android.R.drawable.ic_dialog_map,
"Map",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT, null));
noti = new Notification.BigTextStyle(builder)
.setBigContentTitle("New challenger arrived!")
.bigText(s)
.setSummaryText("You will never win against me").build();
NotificationManager nm = (NotificationManager) MainActivity.this
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);
I think you have to manually cancel the notifications if you take an action. This can be done by calling
notifMgr.cancel(0);
Clicking the notification itself should dismiss it, but for some reason actions don't seem to. The reason you pass 0 as the argument is becasue that's the ID you gave when you called:
notifMgr.notify(0, noti);
That's my updated answer, however, I had previously answered this:
It looks like you're trying to launch the same activity (from your PendingIntent) that you currently have displayed (I'm assuming this is a 1-activity app, with MainActivity containing the code above). Create another activity, build your PendingIntent so it launches that activity, then it should do as you wish. Alternatively, run your activity, notification appears, navigate to homescreen, or different app, then act on your Activity, your desired behavior should manifest.
Related
I am using bundled notifications to group notifications by type. This works quite fine from a user's point of view when notifications are maximised, as shown below:
This does not work too well however when the child notifications are collapsed within the summary:
Now, given that the same REGISTER action can be performed on all the notifications in this group, I would like to give the user the possibility to carry out all these by simply tapping on a REGISTER ALL action in the summary notification. Adding actions to the summary however seems to have no effect. The code below - which is what I'm currently using - shows that the action on the summary notification is simply ignored:
final PendingIntent actionIntent = PendingIntent.getService(this, 0, RegisterAllService.getRegisterAllServiceIntent(),PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Action action = new NotificationCompat.Action(R.mipmap.ic_launcher, "register all", actionIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_notification)
.setAutoCancel(true)
.addAction(action)
.setGroup("confirmgroup")
.setGroupSummary(true);
NotificationManagerCompat.from(getApplicationContext()).notify(0, builder.build());
for(MyNotification myNotification: myNotifications){
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent actionIntent = PendingIntent.getService(this, 0, RegisterService.getRegisterServiceIntent(),PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Action action = new NotificationCompat.Action(R.mipmap.ic_launcher, "register", actionIntent);
NotificationCompat.Builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle(myNotification.getTitle())
.setStyle(new NotificationCompat.BigTextStyle().bigText(myNotification.getSubtitle()))
.setTicker(myNotification.getSubtitle())
.setContentText(myNotification.getSubtitle())
.setAutoCancel(true)
.addAction(action)
.setGroup("confirmgroup")
.setGroupSummary(false)
.setContentIntent(pendingIntent)
NotificationManagerCompat.from(getApplicationContext()).notify(myNotification.getId(), builder.build());
}
Is there a way to make this happen?
I have an app that kicks off a notification on a Wear device (the notification is created and shown on the Wear device).
What I want to do is have the "first" notification page display some high level info (app name, scroll to see more, etc...), and then have a list of pages containing content after that.
That works fine. The issue is that I want to attach an Action to each page (to kick off a PendingIntent). However, no matter what I try, I can't get the page to perform the Action.
I've tried:
setContentIntent
addAction
addAction and extend(new Notification.WearableExtender().setContentAction(0))
Anyone have any ideas?
I'm using Notification, not NotificationCompat, but I don't think that should make a difference.
UPDATE: I'm creating the notification on the watch. Here is the code I use:
private void createNotifications(ArrayList<Thing> things) {
DataApi data = Wearable.DataApi;
int notificationId = 0;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder mainBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.default_icon)
.setContentTitle("Things")
.setContentText("Swipe to see your things")
.setPriority(Notification.PRIORITY_DEFAULT);
List<Notification> pages = new ArrayList<>(things.size());
for(Thing thing : things) {
pages.add(createNotificationPageForThing(data, thing, notificationId).build());
notificationId++;
}
Notification n = new Notification.WearableExtender().addPages(pages).extend(mainBuilder).build();
nm.notify(notificationId, n);
}
private Notification.Builder createNotificationPageForThing(DataApi data, Thing thing, int notificationId) {
Asset bitmapAsset = getBitmapAsset(data, contact);
Intent thingIntent = new Intent(this, WearDetailActivity.class);
thingIntent.putExtra(WearDetailActivity.DETAIL_EXTRA, thing);
PendingIntent thingPendingIntent = PendingIntent.getActivity(this, notificationId, thingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Action action = new Notification.Action(R.drawable.ic_action_social_person, "More details", thingPendingIntent);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_social_person)
.setContentTitle(thing.getDisplayName())
.setPriority(Notification.PRIORITY_DEFAULT)
.addAction(action)
.extend(new Notification.WearableExtender().setContentAction(0));
if(bitmapAsset != null) {
try {
Bitmap b = BitmapFactory.decodeStream(
data.getFdForAsset(connection.getClient(), bitmapAsset).await().getInputStream());
builder.setLargeIcon(b);
} catch (Throwable ignore) {}
}
return builder;
}
#Eliezer, reading at the following line I did not understand what exactly is the behaviour you're experiencing ... explaining it in details might be helpful to debug your problem.
However, no matter what I try, I can't get the page to perform the Action.
Using .addAction() should work in your case. You'd want to use WearableExtender if you were creating the notification from the device, not the watch itself. Just to confirm something obvious - you're passing the .addAction a PendingIntent right?
Here's a code snippet from one of my applications, which accomplishes exactly what you're aiming for - I have just "Swipe for actions" text in the first page, and the next 2 pages perform different actions.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent openIntent = new Intent(this, CommonWearIntentService.class);
openIntent.setAction(CommonWearIntentService.ACTION_OPEN_ON_PHONE);
PendingIntent openPendingIntent = PendingIntent.getService(this, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent addTransactionIntent = new Intent(this, CommonWearIntentService.class);
addTransactionIntent.setAction(CommonWearIntentService.ACTION_ADD_TRANSACTION);
PendingIntent addTransactionPendingIntent = PendingIntent.getService(this, 0, addTransactionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.swipe_for_actions))
.addAction(R.drawable.add_transaction, getString(R.string.add_transaction), addTransactionPendingIntent)
.addAction(R.drawable.common_full_open_on_phone, getString(R.string.common_open_on_phone), openPendingIntent)
.setVibrate(new long[]{500, 500, 500, 1000})
.setLights(Color.BLUE, 3000, 3000)
.setOngoing(true);
notificationManager.notify(ONGOING_NOTIFICATION_ID, builder.build());
Hope this helps!
Per the documentation, you must use NotificationCompat. After you switch to that setContentIntent() looks to be the right way to set the PendingIntent.
Example from the linked docs:
// Create builder for the main notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.new_message)
.setContentTitle("Page 1")
.setContentText("Short message")
.setContentIntent(viewPendingIntent);
// Create a big text style for the second page
BigTextStyle secondPageStyle = new NotificationCompat.BigTextStyle();
secondPageStyle.setBigContentTitle("Page 2")
.bigText("A lot of text...");
// Create second page notification
Notification secondPageNotification =
new NotificationCompat.Builder(this)
.setStyle(secondPageStyle)
.build();
// Add second page with wearable extender and extend the main notification
Notification twoPageNotification =
new WearableExtender()
.addPage(secondPageNotification)
.extend(notificationBuilder)
.build();
// Issue the notification
notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, twoPageNotification);
My app is getting called by an intent that is passing information(pendingintent in statusbar) but when I hit the home button and reopen my app by holding the home button it calls the intent again and the same extras are still there...
is there any way to clear the intent? or check whether it has been used before?
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("reportID", reportID);
intent.putExtra("message", message);
intent.putExtra("toast_show", true);
intent.putExtra("detail_open", true);
intent.putExtra("SplashActivity", true);
PendingIntent pendingIntent = PendingIntent
.getActivity(context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
// Intent.FLAG_ACTIVITY_NEW_TASK
/* get the system service that manage notification NotificationManager */
NotificationManager notificationManager = (NotificationManager) context
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context.getApplicationContext())
.setWhen(when)
.setContentText(message)
.setContentTitle(title)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(message)
.setLargeIcon(largeIcon)
.setDefaults(
Notification.DEFAULT_LIGHTS
| Notification.DEFAULT_VIBRATE
| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
/* Create notification with builder */
Notification notification = notificationBuilder.build();
notificationManager.notify(0, notification);
The problem starts when I receive a notification. If I start the app from the notification, it starts the correct screen and I can use the app => ok. But when I quit the app (with home or back button) it do not appears anymore in the "recent app" list. More precisely:
Does anyone knows how to keep the app in the "recent app" list after being started form a notification?
Update this line and try
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),intent,PendingIntent.FLAG_UPDATE_CURRENT);
What you need is the PendingIntent.FLAG_CANCEL_CURRENT :
If the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it.
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
This will ensure that your latest data will pe present on your Intent.
I used below code for displaying notification in notification Bar.
it worked fine.
But i need to display notification icon dynamically that will come from web service.
How i can do?
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.image,"status message", System.currentTimeMillis());
Intent in = new Intent(Notify.this,CommentU.class);
PendingIntent pi = PendingIntent.getActivity(Notify.this, 0, in, 0);
note.setLatestEventInfo(Notify.this,"NotificationTitle", "You have a new Commentio", pi);
note.number = ++count;
note.vibrate = new long[] { 500l, 200l, 200l, 500l };
note.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFY_ME_ID, note);
Thanks in Advance.
I have one suggestion for you: you have to download that image which you want to show and then you can set that as bitmap: check below code. I have created one BITMAP.
its look link :
for this you have to add android-support-v4.jar
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification").setLargeIcon(BITMAP)
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, test.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(test.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(NOTIFY_ME_ID, mBuilder.build());
for more detail chekc this link.
Removing notifications
Notifications remain visible until one of the following happens:
The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared).
The user clicks the notification, and you called setAutoCancel() when you created the notification.
You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
You call cancelAll(), which removes all of the notifications you previously issued.
Edited: just replace this.
mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification").setLargeIcon(BITMAP)
.setAutoCancel(true)
.setContentText("Hello World!");
just add some icons in your resource folder and then
int myIcon = R.drawable.image;
your logic goes here ....
and change value of icon according to your logic like myIcon = R.drawable.somenewimage;
and then finally set your notification
Notification note = new Notification(myIcon,"status message", System.currentTimeMillis());
Prelude
I'm trying to add a chronometer on the notification. The chronometer is a service. Every second this line is called (continue Thread is a the "running" boolean, timeString is the elaborated String showing the time):
NotificationChrono.updateNotification(getApplicationContext(), continueThread,
NOTIF_ID, timeString, "Chronometer", notificationManager);
This is the NotificationChrono class:
public class NotificationChrono {
static public void updateNotification(Context context, boolean running,
int id, String title, String text,
NotificationManager notificationManager) {
Intent stopIntent = new Intent("com.corsalini.david.barcalc.STOP");
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context,
0, stopIntent, 0);
Intent startIntent = new Intent(
"com.corsalini.david.barcalc.STARTPAUSE");
PendingIntent startPendingIntent = PendingIntent.getBroadcast(context,
0, startIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context)
.setContentText(context.getString(R.string.notif_text))
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_action_alarm_2)
.setAutoCancel(false)
.setOngoing(running)
.setOnlyAlertOnce(true)
.setContentIntent(
PendingIntent.getActivity(context, 10, new Intent(
context, FrontActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), 0))
.addAction(
running ? R.drawable.ic_action_pause
: R.drawable.ic_action_play,
running ? context.getString(R.string.pause) : context
.getString(R.string.start), startPendingIntent)
.addAction(R.drawable.ic_action_stop,
context.getString(R.string.stop), stopPendingIntent);
notificationManager.notify(id, builder.build());
}
}
Problem
Every second the notification is deleted and recreated, visually it means that every second the notification disappears and reappears in the notification list.
What I would want is to just update the TITLE text, not recreating the notification entirely every second. Is it possible?
Be sure to use the same NotificationCompat.Builder builder each time for creating the Notification!
Although the first time you have to set everything, the second time using the Builder you only have to set the value(s) you want to update. After that it's calling notificationManager.notify(id, builder.build()) just like you did. If you use the same ID then the notification gets updated (important!).
Example:
//First time
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentText(context.getString(R.string.notif_text))
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_action_alarm_2)
.setAutoCancel(false)
.setOngoing(running)
.setOnlyAlertOnce(true)
.setContentIntent(
PendingIntent.getActivity(context, 10,
new Intent(context, FrontActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
0)
)
.addAction(running ? R.drawable.ic_action_pause
: R.drawable.ic_action_play,
running ? context.getString(R.string.pause)
: context.getString(R.string.start),
startPendingIntent)
.addAction(R.drawable.ic_action_stop, context.getString(R.string.stop),
stopPendingIntent);
notificationManager.notify(id, builder.build());
//Second time
builder.setContentTitle(title);
notificationManager.notify(id, builder.build());
But you can also use the setUsesChronometer method of the NotificationCompat class. This automatically displays a chronometer using the given timestamp (able to set with setWhen).
You need to mark the notification as "only Arert Once"
Builder#setOnlyAlertOnce(true)
Thanks for the idea PieterAelse!
In my case I had to go with this (build a new notification) for the second and following times:
builder.setContentText(getConvertedTime(millisUntilFinished));
Notification notification = builder.getNotification();
notification.flags = Notification.FLAG_ONGOING_EVENT;
nm.notify(R.string.app_name,notification);
Hope it helps.