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:\
Related
Is it possible to keep expand state by default for setCustomBigContentView?
It gets collapsed by default when I use setCustomBigContentView method even the size lower than default notification height.
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.notification_layout);
NotificationCompat.Builder builder = builder
.setSmallIcon(R.mipmap.ic_launcher_round)
.setCustomBigContentView(contentView)
.setOngoing(true);
Note: Some users blindly search for similar headline instead of understanding the code and situation. I feel sorry for them.
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));
I am using the below snippet to generate notifications in my android app.
private void sendNotification(String contentText, String message) {
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("clear","clear");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent piResult = PendingIntent.getActivity(this, 0, resultIntent,0);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setColor(ContextCompat.getColor(getApplicationContext(),R.color.red))
.setContentTitle("title")
.setContentText(message)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources()
,R.drawable.notification))
.setContentIntent(piResult);
NotificationCompat.InboxStyle notification = new NotificationCompat.InboxStyle(builder);
int i;
for(i=0; i<messageList.size();i++){
notification.addLine(messageList.get(i));
}
notification.setBigContentTitle("title");
notification.setSummaryText(contentText);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID,notification.build());
}
It works in android 5 and 6 but for android nougat it is not working
Following the docs from the Android API:
Status bar icons are composed simply of white pixels on a transparent backdrop, with alpha blending used for smooth edges and internal texture where appropriate.
Your whole image but the transparent parts is going to be converted to white (being originally white or having colors).
One solution is to create a silhouette icon with color, by this way you can use the same image in all Android APIs. One example could be this icon:
In lower versions of Android you would see the black face, in the last versions you will see the same face but with white color. That's because of the transparent parts (it seems SO removes the transparency, you can get the original from here) of the image.
From android version lollpop onwards, they have made the changes for the notifications. When you are specifing the small icon, it should be of specific size as mentioned in this link.
The important thing is the image should be transparent and contains only white color.
You can check this question to get the answer
According to this blog here
It says that
You’ll note that the icons are not present in the new notifications; instead more room is provided for the labels themselves in the constrained space of the notification shade. However, the notification action icons are still required and continue to be used on older versions of Android and on devices such as Android Wear.
If you’ve been building your notification with NotificationCompat.Builder and the standard styles available to you there, you’ll get the new look and feel by default with no code changes required.
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">
....
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 ;-)