I have simple application that sends notifications, which I want to show on Samsung Watch (Active 2). The notification itself is shown on watch, but I cannot get Icon to be displayed. Here is example of notification from gmail and from my app. On gmail (left) you can see icon of the app, while on the right you cannot. What am I missing?
notification-shown
Relevant code I use is below:
int notificationId = 001;
Bitmap bIcon = BitmapFactory.decodeResource( getResources(),R.drawable.ic_stat_ic_notification);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setLargeIcon(bIcon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender().setContentAction(0);
notificationBuilder.extend(wearableExtender);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Home Notifier", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(notificationId /* ID of notification */, notificationBuilder.build());
Related
I can see on MI phone android version 10. i am trying to create a simple notification with title and subtitle. notification also shows up but with no sound and no vibration.
PFB the code:
Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.app_icon_white);
notificationBuilder.setColor(getResources().getColor(R.color.theme_color));
} else {
notificationBuilder.setSmallIcon(R.drawable.app_icon_white);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
CharSequence name = getResources().getString(R.string.feroz_channel_name);
String description = getResources().getString(R.string.feroz_channel_description);
channel.enableLights(true);
channel.setLightColor(getResources().getColor(R.color.theme_color));
channel.enableVibration(true);
channel.setDescription(description);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(77 /* ID of notification */, notificationBuilder.build());
I have vibration permission in Android Manifest. Along with no sound and no vibration. this notification is not visible when device is in locked state. Please suggest what i have missed in the code to achieve this scenario on android 10.
I have implemented custom view for firebase push notification. For custom view we need to remove "notification" key from push Json so that it can be handled even when app is closed like below:
{
"data": {
"detail": {
}
},
"to": ""
}
For creating custom notification I used below code:
private void generateNotification(String title, String message, Intent intent) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
String channelId = getString(R.string.default_notification_channel_id);
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationCount, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
/*
* Custom notification layout
* */
String notificationHeaderText = getResources().getString(R.string.app_name) + " \u2022 "
+ DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME);
RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.view_collapsed_notification);
collapsedView.setTextViewText(R.id.timestamp, notificationHeaderText);
collapsedView.setTextViewText(R.id.content_title, title);
collapsedView.setTextViewText(R.id.content_text, message);
RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.view_expanded_notification);
expandedView.setTextViewText(R.id.timestamp, notificationHeaderText);
expandedView.setTextViewText(R.id.content_title, title);
expandedView.setTextViewText(R.id.notification_message, message);
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.footer_click);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_inkclick_logo_colored)
.setSound(soundUri)
.setGroup(GROUP_KEY_INKCLICK)
.setAutoCancel(true)
.setGroupSummary(true)
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
.setContentIntent(pendingIntent);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (manager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
getResources().getString(R.string.default_notification_channel_id), NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setLightColor(Color.MAGENTA);
channel.setVibrationPattern(new long[]{0, 1000/*, 500, 1000*/});
channel.enableVibration(true);
channel.setShowBadge(true);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.setSound(soundUri, audioAttributes);
manager.createNotificationChannel(channel);
}
manager.notify(0, notificationBuilder.build());
}
else {
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
manager.notify(0, notificationBuilder.build());
}
}
notificationCount += 1;
}
I have added channel.setShowBadge(true); after reading the official notification badge documentation here as well as other answers on stackoverflow like this and this.
I have also tried uninstalling the app and restarting the device but the badge is not showing.
Device is running on API 28(Pie).
You can set style by adding below line
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
add this code
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_inkclick_logo_colored)
.setSound(soundUri)
.setGroup(GROUP_KEY_INKCLICK)
.setAutoCancel(true)
.setGroupSummary(true)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
.setContentIntent(pendingIntent);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
You can set the style of the Android notification badge like this:
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
The overall code can look like this:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_inkclick_logo_colored)
.setSound(soundUri)
.setGroup(GROUP_KEY_INKCLICK)
.setAutoCancel(true)
.setGroupSummary(true)
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setContentIntent(pendingIntent);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
You need to specify the title or text (setContentTitle or setContentText). Because Android SDK uses it to describe notification in the window which appears when you long-click to the app icon. I don't know why this is not specified in the documentation, but it works this way.
I am developing an app in which I am not getting notification when the app is not running in Android Oreo. When a push notification is received, the onMessageReceived method is getting called, but the notification is not showing in the notification bar. Whereas if the app is running (foreground or background) I get a notification in notification bar. public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
Intent intent = new Intent(this, ApplyAndInitiateMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "100";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this,channelId)
.setLargeIcon(bitmap)
.setSmallIcon(R.image_new)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setWhen(remoteMessage.getSentTime())
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(0,"Apply LEAVE",pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).setSummaryText(message).bigLargeIcon(null));
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
Create your notification channel before creating notification:
String channelId = "100";
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setLargeIcon(bitmap)
.setSmallIcon(R.image_new)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setWhen(remoteMessage.getSentTime())
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(0,"Apply LEAVE",pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).setSummaryText(message).bigLargeIcon(null));
Using the following code from the Android Developer docs I am unable to get sound working in the API 27 (Android O) simulator. It works on an API 24 device. I also double checked in the notification settings that the notification channel is set to play the default sound.
Here is a project with the example code below that you can try on the simulators: Github.
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "test-channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel newIncidentChannel = new NotificationChannel(channelId,
"Test Channel",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(newIncidentChannel);
}
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Test")
.setContentText("Text")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
notificationManager.notify("test", NOTIFICATION_ID, notificationBuilder.build());
Update 5/16/18:
I'm using the solution here: https://stackoverflow.com/a/46862503/817886 to use the media play to play sounds when the notification comes in. Not ideal but using this until I can find the proper solution.
Update 5/29/18:
The latest version of Android 8.1.0 has fixed this issue.
You should be set sound in NotificationChannel not in NotificaitonBuilder
For Example
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);
String channelId = "test-channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channelId ,
"Test Channel",
NotificationManager.IMPORTANCE_DEFAULT)
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
context.getString(R.string.app_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
mChannel.setDescription(msg);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes); // This is IMPORTANT
if (mNotificationManager != null)
mNotificationManager.createNotificationChannel(mChannel);
}
Change your
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Test")
.setContentText("Text")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
Like this :
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Test")
.setContentText("Text")
.setDefaults(Notification.DEFAULT_ALL)
.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).
.setAutoCancel(true);
I am using this tutorial to send push notification using Firebase
How can I show Title and Message both in Notification Barenter code here (Still I am getting message as *Title* content in Notification Bar)…
As you can see in this screenshot (due to this, I am unable to deliver complete *message along with Title* to users, because in Notification Bar title has limit maximum 1 line)
I am using Advanced Rest Client (Messaging API) to deliver Notification(s) like this:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIza************adrTY
{ "data": {
"image": "https://ibin.co/2t1lLdpfS06F.png",
"message": "Firebase Push Message Using API"
"AnotherActivity": "True"
},
"to" : "f25gYF3***********************HLI"
}
Code
/**
* Create and show a simple notification containing the received FCM message.
*/
private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.drawable.firebase_icon)
.setContentTitle(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Use
setContentTitle("title") to set the title and of notification
setContentText("text") to set the text.
A example from Android Developers:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
In your case you will have to change your code to:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)
.setSmallIcon(R.drawable.firebase_icon)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setContentTitle(messageTitle) // here
.setContentText(messageBody); // and here
I recommend that you take some time studying NotificationChannel as well, if you wanna to support the new Android O notifications
NotificationChannel example