Action buttons are not shown in notification - android

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).

Related

how to set Notification Button to Icon instead of words

This is my code so far on understanding notification.
Intent mIntent = new Intent(this, typeof(MainActivity));
Intent gIntent = new Intent(this, typeof(GameMenu));
Intent sIntent = new Intent(this, typeof(SceneMenu));
PendingIntent mainIntent = PendingIntent.GetActivity(this, 0, mIntent, 0);
PendingIntent gameIntent = PendingIntent.GetActivity(this, 0, gIntent, 0);
PendingIntent sceneIntent = PendingIntent.GetActivity(this, 0, sIntent, 0);
var notifSound = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
//long[] pattern = new long[] { 1000, 500, 1000, 2000, 3000, 500, 500, 1000};
Notification n = new Notification.Builder(this)
.SetContentTitle("RBOS Notification")
.SetContentText("You won $1million")
.SetColor(Color.LightGreen)
//.SetVibrate(pattern)
.SetPriority((int)NotificationPriority.Max)
.SetShowWhen(true)
.SetSound(notifSound)
//.SetTicker(DateTime.Now.ToString("hh:mmtt"))
.SetSmallIcon(Resource.Drawable.ic_fb)
.SetContentIntent(mainIntent)
.SetAutoCancel(false)
.SetLights(Color.Green, 1000, 1000)
.AddAction(Resource.Drawable.ic_main, "Main", mainIntent)
.AddAction(Resource.Drawable.ic_play, "Game", gameIntent)
.AddAction(Resource.Drawable.ic_scene, "Scene", sceneIntent).Build();
NotificationManager notificationManager =
(NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(0, n);
I am a bit confused on how to set the icon itself for notification rather than words. target api is jellybean to higher.
on my phone Asus with nougat OS, it only display words. In other phone Starmobile and OPPO, it display the icons but with only part of words. I want to display something like radio notif or vlc player notif with previous play/pause next icon buttons alone (without its words).
And for side question. What is the use of ticker? I can't see any changes when I put it there. Also in OPPO, why notification didn't appear instantly (like when battery is low) even priority set in max
Standard notifications will appear different on the various APIs (and even different among the same API level but different OEMs).
It seems you want to create a RemoteViews and use it as your notification's CustomContentView (like a media player in the notification drawer). That way you can override the default notification template using your own layout and content.
Example:
var remoteView = new RemoteViews(PackageName, Resource.Layout.Alarm);
remoteView.SetInt(Resource.Id.myButton, "setText", Resource.String.rocks);
var notification = new Notification.Builder(this)
.SetSmallIcon(Android.Resource.Drawable.IcDialogInfo)
.SetCustomContentView(remoteView)
.Build();
The Resource.Layout.Alarm layout contains just one Button, thus the notification is just one button across the notification drawer:

Notification PendingIntent from BroadcastReceiver

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.

Android click on notification does not open the attached Activity

I want to open an Activity when I click on the notification from the Status bar. I have seen this answered on StackOverflow but none of these answers work for me.
This issue is only seen on Lollipop devices and best way to reproduce is:
1. launch the app.
2. Back ground the app.
3. Receive a push notification.
4. Click on the notification and try with 3-4 push notifications.
Below is my code: I have also tried the following:
adding Intent.ACTION_MAIN
Intent.CATEGORY_LAUNCHER to the intent
PendingIntent.FLAG_UPDATE_CURRENT.
android:exported="true" in my AndroidManifest.xml
but nothing works. I am seeing this issue only on Lollipop devices.
Please note I see this issue intermittently. Is this something related to the intent getting cached and not getting delivered. Please help.
PendingIntent pendingIntent = getPendingIntent(context, payload);
Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat)
.setContentTitle(res.getString(R.string.app_name))
.setContentText(payload.getMessage())
.setTicker(payload.getMessage())
.setContentIntent(pendingIntent)
.setSound(soundUri);
private PendingIntent getPendingIntent(Context context, NotificationPayload payload) {
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(context, DeepLinkActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage(BuildConfig.APPLICATION_ID);
intent.putExtra(NotificationHelper.KEY_NOTIFICATION_PAYLOAD, payload);
return PendingIntent.getActivity(context, requestID, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
}
try adding Intent.FLAG_ACTIVITY_CLEAR_TASK flag in the intent
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
this seems to solve my problem
Have you set the pending intent in the notification setContentIntent method?
Try this, it works for me on all api versions
Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle(getResources().getString(R.string.notification_title))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

Android 4.0.3 notification action is missing

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".

Notification setFullScreenActivity

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

Categories

Resources