I have a small problem with my Android app. It received notifications through FCM and shows them as push notification. So far it is all working, but the strange problem is, sometimes the icon is white and sometimes it is colourful.
When the app is open on the screen and I receive a push notification in this moment, the colourful push notification is shown on top of the screen.
When the app is closed, I get a push notification with a white icon.
I have attached a screenhot:
Screenshot
Here is the code snippet, where the push notification is created:
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setAutoCancel(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_HIGH)
.setColor(Color.parseColor("#83c3ed"))
.setLights(Color.RED, 1000, 500)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.setBigContentTitle("WetterApp");
inboxStyle.addLine(notification.getTitle());
inboxStyle.addLine(notification.getBody());
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
My mobile device has Android 6.0.1, my SDK-version is 23.
Thanks for your help.
Well, I think the problem was something different. My code for creation the notification is only called, when the app is open on screen. When I receive a notification and the app is closed, the notification is handled by the Android system automaticly.
I had to set the color in the notification, which is send to the FCM Server:
$data = [
'notification' => [
'title' => 'Warnung: Wohnzimmer',
'text' => 'Innen: 20,3°C Außen: 24,5°C, Tendenz: -0,2°C',
'color' => '#83c3ed',
'sound' => 'default'
],
'to' => '/topics/testNotification'
];
Now I get a lightblue background icon within the app and also when the app is closed.
Follow the guide of google to create your icons, it's probably that it is occurring in the status bar too.
Then, try this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setTicker(context.getResources().getString(R.string.app_name));
builder.setSmallIcon(R.mipmap.ic_your_status_bar_logo);
builder.setAutoCancel(true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setContentIntent(pendingIntent);
builder.setContentTitle(
context.getResources().getString(R.string.app_name));
builder.setContentText(message);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setColor(ContextCompat.getColor(context, R.color.color_primary));
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, builder.build());
I think that a better solution is to add a silhouette icon to the app and use it if the device is running Android Lollipop.
For instance:
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("My notification")
.setContentText("Look, white in Lollipop, else color!")
.setSmallIcon(getNotificationIcon())
.build();
return notification;
And, in the getNotificationIcon method:
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.icon_silhouette : R.drawable.ic_launcher;
}
Related
This might be duplicate with this question
but still not able to implement notification without heads-up view.
Problem statement : want to show the notification when playing a song. binding one notification with music service that floats on top of the window (Shows a heads-up view) it's very annoying.
Is there any way to disable that heads-up view, just want to show a small icon in status bar.
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL)
.setContentTitle("titile")
.setContentText("desc")
.setSmallIcon(R.drawable.logo)
.setColor(getResources().getColor(R.color.colorPrimary))
.setPriority(NotificationCompat.PRIORITY_LOW)
.setAutoCancel(true);
Notification notification = notificationBuilder.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
CHANNEL, "Halt", NotificationManager.IMPORTANCE_NONE
);
mNotificationManager.createNotificationChannel(notificationChannel);
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
After spending some time, I just uninstall and reinstall the application and start works.
Buggy Rocks
I'm new here. I'm developing an app that is using google maps when i press a FAT button. As at the moment google maps does not indicate when you finished a Navigation and so to be able to return to my application, I am trying to do this in some way.
I was thinking, and google maps when you're in navigation mode, it shows you a notification. Until the navigation is finished, it is not deleted. I wanted to get this notification and when my button was pressed, check if the notification is active. In case it is not active, then I can return to my application.
This is what i've been trying to do:
NotificationManager notificationManager =
GetSystemService(Context.NotificationService) as NotificationManager;
StatusBarNotification[] nnn2 = notificationManager.GetActiveNotifications();
When i check nnn2 to see what services are active, it has nothing. I don't know if i need to add some permissions in manifest, or i'm totally wrong.
This is possible and can be done something like this:
For levels below API 11 :
Notification notify = new Notification();
notify.Defaults = NotificationDefaults.Sound;
notify.Defaults = NotificationDefaults.Vibrate;
notify.Flags = NotificationFlags.NoClear;// Cruicial line of code
For API 11 and above:
Notification notify = new Notification();
notify.Defaults = NotificationDefaults.Sound;
notify.Defaults = NotificationDefaults.Vibrate;
notify.Flags = NotificationFlags.NoClear;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle(messageTitle)
.SetContentText(messageBody)
.SetOngoing(true)//Important line of code
.SetContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(0, notificationBuilder.Build());
I have a some troubles with understanding of goal of setGroup() method.
As docs said :
...Grouped notifications may display in a cluster or stack on devices which support such rendering....
Here it is the first question :
What it is this rendering? What's so special about it?!
I create a method which show a custom text message :
public static void showNotification(Context context, String title, String message, PendingIntent pendingIntent) {
notificationMessages.add(message);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
// .setGroupSummary(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentInfo("" + (notificationMessages.size()))
/*.setGroup(++i + "")*/;
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(title);
for (int i = 0; i < notificationMessages.size(); i++) {
inboxStyle.addLine(notificationMessages.get(i));
}
builder.setContentIntent(pendingIntent);
builder.setStyle(inboxStyle);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
mNotificationManager.notify(0, notification);
}
and play with notificationID, setGroup and setGroupSummary methods.
public static void showNotification(Context context, String title, String message, PendingIntent pendingIntent) {
notificationMessages.add(message);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
// .setGroupSummary(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentInfo("" + (notificationMessages.size()))
.setGroup(GROUP_KEY);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(title);
for (int i = 0; i < notificationMessages.size(); i++) {
inboxStyle.addLine(notificationMessages.get(i));
}
builder.setContentIntent(pendingIntent);
builder.setStyle(inboxStyle);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
mNotificationManager.notify(new Random().nextInt(3), notification);
}
But, no visual changes
comes if I commented lines or not. So here is a stuck for me in understanding of purpose of this method.
from the official docs:
http://developer.android.com/preview/features/notification-updates.html
Android N also allows you to bundle similar notifications to appear as a single notification. To make this possible, Android N uses the existing NotificationCompat.Builder.setGroup() method. Users can expand each of the notifications, and perform actions such as reply and dismiss on each of the notifications, individually from the notification shade.
Meaning the setGroup will only make a difference if the device supports it.
Devices that support it are:
Android Wear devices. when showing remote notifications, you can group together them
Android N. Devices running the Android N developer preview (or in the future the official N release), will show a group of notifications together
The following blog post shows how those work on Android N: https://medium.com/exploring-android/android-n-introducing-upgraded-notifications-d4dd98a7ca92
bellow is a render of that a group looks like:
That means that setGroup will make no difference on devices running anything bellow API23, that includes, Marshamallow, Lollipop, KitKat, etc.
setGroupSummaryis there to support grouped notifications on devices before Nougat. It replaces the single notifications with 1 summary notification.
On Nougat+ the system creates a normal group, which is not your notification with setGroupSummary(true)
I created a notification and it works but not display in Moto Display when locked.
I changed Priority, Category etc with no effects.
I want this notification like messages or missed cals:
moto display
This runs as a service:
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.emblem2)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_help_red))
.setColor(getResources().getColor(R.color.colorFirst))
.setContentTitle(title)
.setContentText(location)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setShowWhen(true)
.setCategory(Notification.CATEGORY_ALARM)
.setLights(Color.YELLOW, 200, 200)
.setContentIntent(pendingIntent);
notificationBuilder.setVibrate((sharedPreferences.getBoolean("notifications_new_message_vibrate", false) ? new long[]{0, 300, 200, 300} : new long[]{}));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(requestCode, notificationBuilder.build());
As pointed out by Marcin, Moto Display doesn't work with vector drawable in notification small icon. I had the same problem and changing the small icon resolved the issue.
It's sad that such a nice feature as Moto Display doesn't have a documentation pointing that out.
Notification notification = new Notification.Builder(context)
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat_player)
// Add media control buttons that invoke intents in your media service
.addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0
.addAction(R.drawable.ic_pause, "Pause", pausePendingIntent) // #1
.addAction(R.drawable.ic_next, "Next", nextPendingIntent) // #2
// Apply the media style template
.setStyle(new Notification.MediaStyle()
.setShowActionsInCompactView(1 /* #1: pause button */)
.setMediaSession(mMediaSession.getSessionToken())
.setContentTitle("Wonderful music")
.setContentText("My Awesome Band")
.setLargeIcon(albumArtBitmap)
.build();
That worked in my Moto Z, maybe only work in Media mode.
I've just updated my ic_launcher in my android application (made in Android Studio, btw). I'm trying to create a test notification, and it used to work, but now it doesn't and I don't know why.
Notification.Builder builder = new Notification.Builder(this);
builder
.setWhen(System.currentTimeMillis())
.setContentTitle("TEST")
.setContentText("Hello! This is a notification test!")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
NotificationManager NoMa = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NoMa.notify(1, builder.build());
It doesn't even work if I remove the setLargeIcon line, which in my point of view makes no sense. I don't discard the possibility that there's something wrong with my code. Thanks.
You have to setSmallIcon() if you want to show any notification.
So change your code to
Notification.Builder builder = new Notification.Builder(this);
builder
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher) //Any icon you want
.setContentTitle("TEST")
.setContentText("Hello! This is a notification test!")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
NotificationManager NoMa = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NoMa.notify(1, builder.build());