I'm creating notification like this:
NotificationManager notifyManager = (NotificationManager) App
.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
App.getContext());
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle(task.getFileNode().getName());
Intent cancelIntent = CancelDownloadReceiver
.getCancelDownloadIntent(ticketUUID);
PendingIntent intent = PendingIntent.getBroadcast(
App.getContext(), 0, cancelIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(
R.drawable.ic_stat_cancel,
App.getContext().getString(
R.string.notification_download_cancel),
intent);
builder.setProgress(0, 0, true);
Notification notification = builder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
notifyManager.notify(ticketUUID, 1, notification);
The issue is that on Motorola XOOM with Android 4.0.3 action is missing. Why? As you can see I'm using classes from support library.
Even though this is in the support library, it won't work on Android 4.0.x. See documentation: "Action buttons won't appear on platforms prior to Android 4.1".
Related
guys.
I've been debugging this for days, but I can't find an answer for this.
As the title says, I am using BroadcastReceiver to make my notification.
Everything was fine until I add .addAction.
It needs PendingIntent for default and I made a PendingIntent.
But the problem is this; when I add .addAction, It says this
Incompatiable types.
Required : android.app.Notification
Found : android.app.Notification.Builder
the code works perfectly fine with out the .addAction and the PendingIntent.
What could be the problem?
Here is my code,
BroadcastReceiver, WifiService.java
Intent intent = new Intent(mcontext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(mcontext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti = new Notification.Builder(mcontext)
.setContentTitle("Welcome home!")
.setContentText("You have a to-do; " + hometodo)
.setSmallIcon(R.drawable.home_light)
.setLargeIcon(icon)
.setLights(0xFFFFD800, 5000, 0)
.addAction(R.drawable.archive_notification, "Mark as done", pIntent);
//.addAction gets the error.
.build();
NotificationManager notificationManager = (NotificationManager) mcontext.getSystemService(mcontext.NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
used NotificationCompat for setting .addAction(...).
Check official docs
action buttons won't appear on platforms prior to Android 4.1. Action
buttons depend on expanded notifications, which are only available in
Android 4.1 and later.
I'm trying to create a notification, without using the NotificationCompat.Builder. The motivation is not using the Support Library if its not really needed (and also for educational purposes)
I assume that its possible, as there are "native" Notification and NotificationManager classes that are exposed to the users.
My try goes as follow:
Intent i = new Intent();
i.setClassName("com.test", "com.test.SomeActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
notification.setLatestEventInfo(this, title, content,pi);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0 , notification);
This code just run, but no notification appears, and notificationManager.notify() in general returns void.
I want to display action buttons in notification bar. Notification works but I cannot see any button . I have read Android Developer document and different examples on the Internet but I could not find any big difference with my code. I have done like following:
public void showNotification(Context context) {
NotificationManager mNotifyMgr = (NotificationManager);
context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder = new NotificationCompat.Builder(context);
Intent prevIntent = new Intent(mContext, PlayAudioService.class);
prevIntent.setAction(ACTION_PREV);
PendingIntent piPrev = PendingIntent.getService(mContext, 0, prevIntent, 0);
Intent playIntent = new Intent(mContext, PlayAudioService.class);
playIntent.setAction(ACTION_PLAY);
PendingIntent piPlay = PendingIntent.getService(mContext, 0, playIntent, 0);
Intent nextIntet = new Intent(mContext, PlayAudioService.class);
nextIntet.setAction(ACTION_NEXT);
PendingIntent piNext = PendingIntent.getService(mContext, 0, nextIntet, 0);
mBuilder.setSmallIcon(smallIcon)
.setContentTitle("Title")
.setContentText("Text")
.setTicker("Ticker")
.setWhen(0)
//.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.addAction (R.drawable.ic_previous, "Prev", piPrev)
.addAction (R.drawable.ic_play, "Play", piPlay)
.addAction (R.drawable.ic_next, "Next", piNext);
Intent notifyIntent = new Intent(context, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent piNotify =
PendingIntent.getActivity(
mContext,
0,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(piNotify);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
In activity:
showNotification(this);
My Android target SDK version 15 and I am using latest version of support library v4. What am I missing and did not understand properly?
Your answer would be appreciated.
Notification Actions were introduced in API 16.
This means that you should be setting your target SDK to 16 or above.
However, only Jelly Bean and above can take advantage of the Notification Actions - make sure to test this feature on those platforms.
NotificationCompat.Builder takes care of generating a Notification compatible with whatever API it is running on, so keep using it if you are going to be supporting Ice Cream Sandwich and older. If not, simply using Notification.Builder will suffice (after changing the target SDK of course).
I have the following code, that's suppose to initialize a new activity during the notification, this is located in a service class
Intent push = new Intent();
push.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
push.setClass( context, MyActivity.class );
PendingIntent pi = PendingIntent.getActivity( context, 0, push, PendingIntent.FLAG_ONE_SHOT );
long[] vibraPattern = {0, 500, 250, 500 };
Notification noti = new Notification.Builder( getApplicationContext() )
.setVibrate( vibraPattern )
.setDefaults( Notification.DEFAULT_SOUND )
.setFullScreenIntent( pi , true )
.setContentIntent( pi )
.getNotification();
notifMng.notify( 0 , noti );
The sound and the vibration go nicely, so the noti is successfully notified, however MyActivity is never created, even while it is the FullScreenIntent of this notification.
Notifications are tricky with the API changing so much between different versions. Your solution is targeting API level 11+ (3.0.x) and does not work on any 2.x devices. The getNotification() method is also deprecated in 4.1...
Your notification is missing content to show. It will actually launch the activity when the notification is received but does not display notification because it does not have any content to display.
Add .setFullScreenIntent( pi , true ) if you want to launch an activity immediately upon receiving the push.
I have modified your code so that it works:
Intent push = new Intent();
push.setClass(context, MyActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, push,
PendingIntent.FLAG_ONE_SHOT);
long[] vibraPattern = { 0, 500, 250, 500 };
Notification noti = new Notification.Builder(getApplicationContext())
.setVibrate(vibraPattern)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pi)
.setContentTitle("Title")
.setContentText("content text")
.setSmallIcon(R.drawable.ic_launcher)
.getNotification();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
Notification.Builder requires API 11+. Use NotificationCompat.Builder instead, which is supported only requires API 4+, and allows for the implementation of new features (i.e. new notification styles available in Jelly Bean) on supported devices, while older devices just ignore the new features. It makes everything much smoother.
See this link for more details: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
you need following three variables to pass:
the color
if turning on the led is on
if turning off the led is on
if you enable 2 and 3 your led will be blinking, if you disable 2 and 3 the led will be turned off
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context );
mBuilder.setLights(Color.RED, 1, 1); // will blink
I just want to make a custom notification on statusbar that remanis there during user interaction.
I use this code:
Notification notification = new Notification(getBatteryStateResource(percent,status), "notify",System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.statusbar);
Intent intent = new Intent(mContext, Activity.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
notification.contentIntent=contentIntent;
notification.contentView.setOnClickPendingIntent(R.id.imageView, anotherContentIntent);
NotificationManager nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(42, notification);
This code work only on android 4 smartphones. On tablets, every time I click on the imageView of the notification, system delete notification.
Instead, on Android 2.3 and 2.2 the "anotherContentIntent" don't start and start only "contentIntent", why??
many thanks at all....
Use FLAG_NO_CLEAR in addition to FLAG_ONGOING_EVENT.