Android Notification with many actions are not shown - android

i am trying to show a notification with more than 3 actions. Unfortunately, the forth action and so on are not showing (probably because there not enough space). Also, the action items does not have the same width.
Does anyone know how can i display more than 3 actions?
This is my code:
final NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(message.getData().get(DATA_TITLE));
inboxStyle.addLine(message.getData().get(DATA_BODY));
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle(message.getData().get(DATA_TITLE))
.setContentText(message.getData().get(DATA_BODY))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(inboxStyle)
.setContentIntent(defaultIntent)
.setPriority(Notification.PRIORITY_MAX);
addActions(notificationBuilder, message);
private void addActions(final NotificationCompat.Builder builder, final RemoteMessage message) {
if (containsAction(message, EventActionType.OpenMessage)) {
builder.addAction(R.drawable.ic_email, "open", getActionIntent(message, MyActivity.class));
}
if (containsAction(message, EventActionType.Details)) {
builder.addAction(R.drawable.ic_notification_account, "details", getActionIntent(message, MyActivity.class));
}
if (containsAction(message, EventActionType.Transfer)) {
builder.addAction(R.drawable.ic_access_time, "transfer", getActionIntent(message, MyActivity.class));
}

This link can help you.
According to standard docs of android we can't have more than three actions for a notification.
If you want to have more than three actions , you can use remoteViews. Every notification will have a default view which is provided by android os, but we can customise it. To do that we need to create a layout will be used as a our notification view and use as many as buttons as you want in that layout, but height of the layout is limited as notification height is limited by android.Make sure all your buttons will fit in notification.Create a remoteViews with this layout.
After that when you are creating notification attach it to notification using setCustomContentView. For each button in the view you can have different pendingintent i.e on clicking on different buttons different pending intents can be executed. see RemoteView.setOnClickPendingIntent(view,pendingIntent).
Happy coding ;-)

Related

Notification icon not showing properly

I am trying to set a new notification icon for my app, however i cannot seem to make it work. I receive the notifications, but instead of my logo i see a black/grey circle in the status bar like this:
My MessageService looks like this:
public class MyMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MessageActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("notification_msg_id", remoteMessage.getData().get("my_msg_id"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder
= new NotificationCompat.Builder(this)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_stat_my_logo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setColor(getResources().getColor(R.color.colorBlack))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
I generated the small icon with this (pretty great) tool and as you can see Android Studio is able to find the image in every size needed:
Here's my SDK versions:
I read earlier that there might be some problems with caching of drawables, i've tried invalidating every cache and cleaned/builded the project as well.
I've also tried different image files as well as xml vector drawables.
Do you guys have any idea what i'm doing wrong or how i can troubleshoot the issue further?
Thanks in advance!
Edit:
This is the icon i am trying to use in xxxhdpi-v11 format (it's white you probably need to click it to see it):
you got the same logo name in each folder maybe the one needed by the notifcation is different as notification icon must be of 24x24 dp
Notification icons must be entirely white for API 21 et later. try using this:
notification.setColor(getResources().getColor(R.color.notification_color));

Android Lock Screen Notification Custom View with Ripple and Double Tap

I am working on an Android app. This last makes use of a notification with a custom view that is displayed on the lock screen. Unfortunately, I am not able to get the ripple and elevation effect when I tap on it like other notifications. Also, a single touch trigger the intent I have configured whereas other notifications require double tap.
I have put a minimal project example on Github:
https://github.com/lpellegr/android-notification-custom-example
The app example offers two buttons to publish notifications: one that uses a custom view and suffer from the issues mentioned above and another notification that uses the default system view with the expected behaviour.
Any idea about how to get the ripple and elevation effect but also the double tap behaviour (by keeping the custom view) is welcome.
PS: I am targeting API 19+ and I want to use a custom view layout for the notification, along with setOnClickPendingIntent since only this listener allows to open an activity whatever the security mode of the device is.
Remove setOnClickPendingIntent from the method publishNotificationWithCustomView and add setContentIntent to the notification builder:
private void publishNotificationWithCustomView() {
String title = "Notification Custom View";
String content = "No ripple effect, no elevation, single tap trigger";
Context context = getApplicationContext();
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setWhen(System.currentTimeMillis())
.setDefaults(DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOnlyAlertOnce(true)
.setAutoCancel(false)
.setColor(ContextCompat.getColor(context, R.color.colorAccent))
.setContentTitle(title)
.setContentText(content)
.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(createLockscreenNotificationPendingIntent(context));
int notificationLayoutResId = R.layout.lock_screen_notification;
// using folder layout-vX is having issue with LG devices
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
notificationLayoutResId = R.layout.lock_screen_notification_android_n;
}
RemoteViews remoteView = new RemoteViews(
context.getPackageName(), notificationLayoutResId);
remoteView.setTextViewText(R.id.title, title);
remoteView.setTextViewText(R.id.text, content);
builder.setCustomContentView(remoteView);
Notification notification = builder.build();
publishNotification(context, notification, 7);
}
Then remove android:clickable="true" from lock_screen_notification.xml and lock_screen_notification_android_n.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="64dp">
....

Expandable Lockscreen Notification?

On Android 6 I'm trying to display a notification with the following combination of properties:
text title and content
expandable image content (see here and here)
don't show a notification icon in the status bar (don't want to clutter it up)
don't flash any LEDs (don't want to trouble user)
show on lockscreen
show on lockscreen in expanded (or at least expandable) format (showing the bigContentView)
I can achieve 3 and 4 by setPriority(Notification.PRIORITY_MIN) but then the notification doesn't seem to show on the lock screen at all (fail on 5).
As for 6, when the notification does shown on the lockscreen e.g. using PRIORITY_MAX (passing on 5 but failing on 3 and 4), it is not expanded or even expandable (failing on 6).
I'm using the following to set up the notification:
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(titleText)
.setContentText(contentText)
.setSmallIcon(R.drawable.small_icon)
.setOngoing(true)
.setPriority(Notification.PRIORITY_DEFAULT)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.build();
// add the image content (via a remoteViews)...
notification.bigContentView = remoteViews;
notificationManager.notify(tag, id, notification);
.setContentView(RemoteViews views)
RemoteViews
- A class that describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.
Edit:
Call .build() later on.
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(titleText)
.setContentText(contentText)
.setSmallIcon(R.drawable.small_icon)
.setOngoing(true)
.setPriority(Notification.PRIORITY_DEFAULT)
.setVisibility(Notification.VISIBILITY_PUBLIC);
// add the image content (via a remoteViews)...
notification.bigContentView = remoteViews;
notificationManager.notify(tag, id, notification.build());

Can't add page to Android Wear notification without the card background

It's quite possible Android Wear just doesn't support this but there seems there should be some workaround. I want to add a custom second page to a notification, but I don't want it to have the white card background.
Here's how I create my notifications:
Intent secondPageIntent = new Intent(this, SecondPageActivity.class);
PendingIntent secondPagePendingIntent = PendingIntent.getActivity(this, 0, secondPageIntent, 0);
Notification secondPageNotification = new NotificationCompat.Builder(this)
.extend(new NotificationCompat.WearableExtender()
.setDisplayIntent(secondPagePendingIntent)
.setHintShowBackgroundOnly(true)
)
.build();
Intent firstPageIntent = new Intent(this, FirstPageActivity.class);
PendingIntent firstPagePendingIntent = PendingIntent.getActivity(this, 0, firstPageIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.extend(new NotificationCompat.WearableExtender()
.setDisplayIntent(firstPagePendingIntent)
.setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.background))
.addPage(secondPageNotification)
);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(curNotificationId++, builder.build());
I've tried:
Setting setHintShowBackgroundOnly which doesn't do anything
From within SecondPageActivity, try to grab parentActivity and set it's alpha to 0. Doesn't work, parentActivity is null.
Calling setCustomContentHeight(0) doesn't remove the card, it just gets skinny
I tried not using a second page but instead launching an activity when the user swipes but it doesn't look good
I really have no idea what to try next. I'm an experienced engineer but pretty new to Android. Any ideas or suggestions would be helpful.
Thanks!
If you want to get rid of the white card you have to set
setCustomSizePreset(WearableExtender.SIZE_FULL_SCREEN)
So instead of something like that:
your custom content will appear on entire screen (without the card decoration).
Please notice that the background of your custom activity is defined by the style declared in manifest. Unfortunately any theme with transparent background won't work, so the background needs to be opaque:(
This is submitted as an issue here: https://code.google.com/p/android/issues/detail?id=73900
I really hope that they will allow transparent backgrounds there in future:\

How to create Custom Notification in android

I need to create a custom notification instead of the default notification in android.
Current notification have a icon,heading and message like below image
I want it customise like this
how can i achieve this
Notification views
Normal View - A notification in normal view appears in an area that’s up to 64 dp tall. Even if you create a notification with a big view style, it will appear in normal view until it’s expanded.
Content title
Large icon
Content text
Content info
Small icon
Notification time
Normal View Like
Big View - A notification’s big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications were first introduced in Android 4.1 JellyBean [API 16]. Expandable notifications were designed to support rich notification style objects called Notification.Style.
Big View Like
Go to this link expandable-notifications-android
More information on official docs
I have create notification usingby following http://developer.android.com/wear/notifications/creating.html
Add code for create notification.
// Specify the 'big view' content to display the long
// event description that may not fit the normal content text.
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(eventDescription);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setLargeIcon(BitmapFractory.decodeResource(
getResources(), R.drawable.notif_background))
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map,
getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle);
That's called Expanded layouts which is available right from Jelly Bean version.
There are 2 views of Notifications:
Normal View
Big View
A notification’s big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications were first introduced in Android 4.1 JellyBean [API 16].
In your case, you just want to display big picture in notification, give Notification.BigPictureStyle a try:
Bitmap remote_picture = null;
// Create the style object with BigPictureStyle subclass.
NotificationCompat.BigPictureStyle notiStyle = new
NotificationCompat.BigPictureStyle();
notiStyle.setBigContentTitle("Big Picture Expanded");
notiStyle.setSummaryText("Nice big picture.");
try {
remote_picture = BitmapFactory.decodeStream(
(InputStream) new URL(sample_url).getContent());
} catch (IOException e) {
e.printStackTrace();
}
// Add the big picture to the style.
notiStyle.bigPicture(remote_picture);
you can use NotificationCompat that need remoteview . below i set my custom layout named
activity_downlaodactivity
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.activity_downlaodactivity);
android.support.v4.app.NotificationCompat.Builder mBuilder =
new android.support.v4.app.NotificationCompat.Builder(this)
.setContent(contentView)
manager.notify(0, mBuilder.build());

Categories

Resources