Android custom notification - android

How to make a custom notification the like a picture attachment.enter image description here
Do you have any suggest or idea for it?

You can use RemoteViews for this
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.YOUR_CUSTOM_LAYOUT);
contentView.setImageViewResource(R.id.YOUR_IMAGEVIEW_ID, YOUR_ICON);
contentView.setTextViewText(R.id.YOUR_TEXT_VIEW_ID, YOUR_NOTIFICAITON_TEXT);
You can add more parameters and customize it more, you have to read the RemoveViews documentation.
Then you create a NotificationCompat.Builder and add contentView as a content from NotificationCompat.Builder something like this :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(YOUR_CONTEXT)
.setContent(contentView);
Then create the Notification
Notification mNotification = mBuilder.build();
mNotificationManager.notify(1, mNotification);
Those are the simple steps, now you can adjust it to your liking.
Also you can see this example on YouTube or this Tutorial

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 Notification with many actions are not shown

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

android wear notification with different text styles

On Add a Big View Google show how to make use of BigTextStyle. The screenshot looks very promising:
So I can see a title "Emmet Connolly" which I can set with setContentTitle().
And I can see a bold header "Re: wearables" which I don't know how to set.
Finally there is some text "Our devs are goint to be building a lot of really awesome apps" which should be set with setContentText().
I played around with some setters from NotificationCompat.BigTextStyle and I can summarize that some are ignored. Seems to be a bug ?!
final NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
.setBackground(BitmapFactory.decodeResource(context.getResources(), R.drawable.chalk));
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("title")
.setContentText("text") // overwritten by bigText()
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("big text")
.setBigContentTitle("big content title") // ignored
.setSummaryText("summary text")) // ignored
.setContentIntent(viewPendingIntent)
.extend(wearableExtender);
final NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(notificationId, builder.build());
Html seems to work:
.setContentText(Html.fromHtml("<b>Best</b> restaurant <i>in</i> <font color=\"#0000ff\">San</font> Francisco <a href=\"http://www.stackoverflow.com\">#food"))
An here is a useful link: HTML Tags Supported By TextView

Set background color of wearable notification

I want to set the background color of a notification in a wearable device. The only method that I found is setBackground(Bitmap background) of NotificationCompat.WearableExtender but this is only for bitmaps. Is it a good idea to create a bitmap rectangle and use this as the background of my notification?
Any help would be appreciated.
Use NotificationCompat.Builder, in this one you can use setColor and add it to NotificationCompat.WearableExtender with the method .extend(). like this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("Title");
builder.setColor(COLOR HERE);
NotificationCompat.WearableExtender builderExtended = new NotificationCompat.WearableExtender();
builderExtended.extend(builder);
builder.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:\

Categories

Resources