Notification image never displays in Android - android

If suppose I create Notification icons by uploading a png image using Image Asset tool from Android Studio by choosing type as notification would it generate all required icons with the dimensions as needed by notification icon spec? if Yes, then there should not be any problem displaying this icon in the notification bar, isn't it? hmm it doesn't in my case but rather it shows the default App icon no matter what. Below is my code. Any guess why its failing, perhaps this is just a cosmetic issue through.
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("VEDRISE",
"Vedic Notifications",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Will display daily Panchang notificatons");
mNotificationManager.createNotificationChannel(channel);
}
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// URL url = new //URL("https://66.media.tumblr.com/ec76b7d7e0529af6e3a437edb6c6c255/tumblr_phiur//jqePq1xp0noco1_75sq.png");
// Bitmap image = //BitmapFactory.decodeStream(url.openConnection().getInputStream());
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "VEDRISE")
.setSmallIcon(R.drawable.sunrise) // notification icon
// .setLargeIcon(image)
.setContentTitle(title) // title for notification
.setContentText(content)// message for notification
.setSound(alarmSound) // set alarm sound for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(context, this.getClass());
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
int id = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
Log.d("VedicHoroo", String.format("id: %d", id));
mNotificationManager.notify( id, mBuilder.build());
Just to give you some scope, this requirement is for local notification which will trigger by the AlarmReceiver. The receiver is triggered by Alarm without issues but the Notification does not display image which is set by setSmallIcon instead it display only the App Icon

Notification notification = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.build();
Even if ic_notification is transparant and white. It must be also defined in the Manifest meta data, like so:
<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_notification" />
add this line in the manifest.xml file in application block

Related

Not getting Large Icon when app is not in background

I am setting large icon for push notification by using following code.
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(notification.getTitle())
.setContentText(notification.getBody())
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.green))
.setSound(defaultSoundUri)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo))
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent);
This works fine and shows largeIcon when app is in foreground, but when app is not in foreground, it does not display large icon.
I am testing the app in samsung s7(oreo)
The code snipped you posted works fine only when your app is in the foreground because it only gets called when the app is in the foreground. When the application is in the background, the Android system displays the notification for you (based on the 'notification' object in the JSON object that the server sends to the device). Your receiver is not being called at all. Currently, Firebase Cloud Messaging doesn't support setting a large icon as a payload in the request.
A way around this is to not include a 'notification' object in the POST payload of the message. When you only include 'data' object in the payload, your receiver will be asked to handle the notification even when your application is in the background. Then, you can build the notification the same way as if the app was in the foreground and set a large icon for the notification.
Take a look at this answer for a more detailed explanation about this issue.
Try this,
you're recommended to set default values to customize the appearance of notifications. You can specify a custom default icon and a custom default color that are applied whenever equivalent values are not set in the notification payload.
Add these lines inside the application tag to set the custom default icon and custom color:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_stat_ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
Set your application icon transparent
android:icon="#drawable/icon_transper"
and add this lime in AndroidManifest
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/gardi" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
If you set your application icon transparent it can take as notification icon in background.
prefer this link to generate notification icon.
https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=clipart&source.clipart=ac_unit&source.space.trim=1&source.space.pad=0&name=ic_stat_ac_unit
I hope this can help you!
Thank You.
It's because you've to set the large icon first and small icon second to that, also you shouldn't use vector images as large icon. Use either Jpeg or PNG images.
new NotificationCompat.Builder(this, channelId)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo))
.setSmallIcon(R.drawable.logo)
.setContentTitle(notification.getTitle())
.setContentText(notification.getBody())
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.green))
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent);
try once.. Where are you storing your icon in drawable folder .. all the folders like hdpi, xhdpi, xxhdpi.. pls try this once..
You need to check version. If version >= Oreo than you need to add channel id. Please see the sample code below which is working for me:
public void createNotification(Bitmap bitmap, String aMessage, Context context, int icon, String titles, String noti_id, String mid, String att_url, String mname) {
NotificationManager notifManager = null;
final int NOTIFY_ID = 0; // ID of notification
String id = context.getString(R.string.default_notification_channel_id); // default_channel_id
// String title = context.getString(R.string.default_notification_channel_id); // Default Channel
String title = titles;
Intent intent;
PendingIntent pendingIntent;
NotificationCompat.Builder builder;
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
String msg = aMessage;
// bigPictureStyle.setSummaryText("Summary text appears on expanding the notification");
bigPictureStyle.setSummaryText(msg);
bigPictureStyle.bigPicture(bitmap);
if (notifManager == null) {
notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
String m_namse = "Marwadi University - " + mname;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notifManager.getNotificationChannel(id);
if (mChannel == null) {
mChannel = new NotificationChannel(id, title, importance);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notifManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(context, id);
intent = new Intent(context, FullScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// List<String> test = new ArrayList<String>();
// test.add();
intent.putExtra("title", titles);
intent.putExtra("body", aMessage);
ConstantData.setNotificaitonId(context);
// ConstantData.getNotificaitonId(context);
bundle.putString("values", str);
intent.putExtra("img_path", imageUri);
intent.putExtra("n_id", noti_id);
intent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentTitle(aMessage) // required
.setSmallIcon(R.drawable.mu_top_white) // required
.setContentText(m_namse) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setStyle(bigPictureStyle)
.setContentIntent(pendingIntent)
.setTicker(aMessage)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
} else {
builder = new NotificationCompat.Builder(context, id);
// intent = new Intent(context, AllNotificationActivity.class);
intent = new Intent(context, FullScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("title", titles);
intent.putExtra("body", aMessage);
// intent.putExtra("img_path", imageUri);
intent.putExtra("img_path", imageUri);
intent.putExtra("n_id", noti_id);
intent.putExtra("mid", mid);
ConstantData.setNotificaitonId(context);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentTitle(aMessage) // required
.setSmallIcon(R.drawable.mu_top_white) // required
.setContentText(m_namse) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setStyle(bigPictureStyle)
.setContentIntent(pendingIntent)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setTicker(aMessage)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setPriority(Notification.PRIORITY_HIGH);
}
Notification notification = builder.build();
notifManager.notify(NOTIFY_ID, notification);
}
And then Simple call this method:
createNotification(bitmap, message, mContext, R.drawable.main_logo, title, noti_id, mid, att_url, msg);

Custom notification sound not working in 5.0 and higher

I am trying to add custom sound to notification. Following is my code:
notificationSoundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.error);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default",
"YOUR_CHANNEL_NAME",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("YOUR_NOTIFICATION_CHANNEL_DISCRIPTION");
mNotificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "default")
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle("BATTERY FULL") // title for notification
.setContentText("Battery is full. Please plug out the charger. Overcharging may decrease battery life span.")// message for notification
.setSound(notificationSoundUri) // set alarm sound for notification
.setAutoCancel(true); // clear notification after click
mNotificationManager.notify(1, mBuilder.build());
This code is working in pre-lollipop devices and not on Marshmallow. In Marshmallow, it is using phone's default notification sound. I am assuming this problem is for lollipop and higher devices. What am I missing here?
Had this issue for a few tries, it looks like NotificationCompat misses a method for sound on between androids 21 and 25, for notification to work you need to set the sound as this
val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(this, channelId1)
} else {
Notification.Builder(this)
}
...
val audioAttr = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM).build()
builder.setSound(
notification1Sound(), // custom uri
audioAttr)
I don't support versions prior lollipop so this works for me, for older version you might need to add a second version check
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setSound(
notification1Sound(),
audioAttr
)
} else {
builder.setSound(notification1Sound(), AudioManager.STREAM_ALARM)
}
You have to use Notification Channel whit Android O (https://developer.android.com/training/notify-user/channels).
This work only on API 26+ because the NotificationChannel class is new and not in the support library.
builder.setStyle(new NotificationCompat.InboxStyle());
The full code:
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, MenuScreen.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);
builder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
builder.setVibrate(pattern);
builder.setStyle(new NotificationCompat.InboxStyle());
Use these lines of code for custom sound
URI uri=Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.FILE_NAME);//Here is FILE_NAME is the name of file that you want to play
builder.setSound(uri);
Don't forget to add notification channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
#SuppressLint("WrongConstant")
NotificationChannel channel = new NotificationChannel("XYZ", "ABC",NotificationManager.IMPORTANCE_MAX);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(0, mBuilder.build());

Notification manager has stopped working

This issue has been solved. See my solution below.
I just completed converting my messaging app to FCM. You can see the process I have been through here. Now that it is done, my notifications no longer work. If my FirebaseMessagingService gets a message and the main app is not active, I create a notification on the phone I'm running on.
This has been running for years correctly on GCM. When I trace through the code, it all executes ok - just no notification shows up in the tray. I can't imagine what Firebase would have to do with this.
This is the code that gets called from the FirebaseMessagingService. This code has been running for years just fine . . .
public static void raiseNotification( String username, String mesText, int count)
{
String message = "From: " + username + " " + mesText;
if (count > 1)
{
count--;
message = message + " + " + count + " more";
}
NotificationCompat.Builder b = new NotificationCompat.Builder(GlobalStuff.GCT);
Intent intent = new Intent(GlobalStuff.GCT, MainActivity.class);
intent.putExtra("whattodo", username);
intent.setAction(Long.toString(System.currentTimeMillis())); //just to make it unique from the next one
PendingIntent pIntent = PendingIntent.getActivity(GlobalStuff.GCT, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
b.setContentTitle("New SafeTalk Message")
.setSmallIcon(R.drawable.ticon)
.setContentText(message)
.setTicker("New SafeTalk Message")
.setContentIntent(pIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);
//.addAction(R.drawable.smallredball, "Read Now", pIntent)
//.addAction(R.drawable.smallquestion, "hello there", pIntent);
NotificationManager mgr = (NotificationManager)GlobalStuff.GCT.getSystemService(NOTIFICATION_SERVICE);
mgr.notify(0, b.build());
}
This issue is solved. Once you write an app for Android, Google will have you working full time for the rest of your life just to keep it working. They are breaking change demons. Turns out this notification problem has nothing to do with Firebase (which is itself the mother of all breaking changes).
Google changed the requirements on how to send a notification in Oreo. Google designed this change so that if your app is running on Oreo and you haven't made the change your notification simply won't work - hope nobody was building notifications that were important. In Oreo they require a channelId.
Here is code that works in Oreo . . .
Actually this code does not completely work in Oreo. See my next post regarding notifications in Oreo
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.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);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
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 /* ID of notification */, notificationBuilder.build());
}

Notification background image never shown on clock

I don't get it manages to show the background I did that based on the ElizaChat example but I don't get the background it is always black.
Here is my code:
public static void createNotification(Context context) {
int notificationId = 1;
NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText("I am a big style message");
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
//.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("hallo")
.setContentText("I am a message")
.setLargeIcon(BitmapFactory.decodeResource(
context.getResources(), R.drawable.clock_bg))
//.setStyle(bigStyle)
;
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Notification notification =
new WearableNotifications.Builder(notificationBuilder)
.setHintHideIcon(true)
.setMinPriority() // show only on clock
.build();
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notification);
}
Any Idea?
This is my output:
The right way is to use the WearableExtender like in this short example:
NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
Bitmap bg = BitmapFactory.decodeResource(context.getResources(), background);
extender.setBackground(bg);
notificationBuilder.extend(extender);
Original Answer / alternative
I found a solution I need to use the BigPictureStyle like this:
Bitmap background = BitmapFactory.decodeResource(context.getResources(),
R.drawable.clock_bg);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setContentTitle("hallo")
.setContentText("I am a message")
.setLargeIcon(background)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(background));

Android Notification LED doesn't use my Color

I'm trying to use a Notification that also uses the Notification LED on my S3, but for some reason the color will always be blue (I guess it's the default?). I tried to use different colors but nothing changes. Other apps (like Whatsapp, Gmail and Facebook have no problems with showing a different color light).
Notification.Builder noteBuilder = new Notification.Builder(context)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_DEFAULT)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(ContentTitle)
.setContentText(ContentText)
.setLights(Color.YELLOW, 500, 500)
;
Intent noteIntent = new Intent(context,HoofdScherm.class);
PendingIntent notePendingIntent = PendingIntent.getActivity(context, 0, noteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
noteBuilder.setContentIntent(notePendingIntent);
Notification note = noteBuilder.build();
note.ledARGB = Color.YELLOW;
NotificationManager mgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mgr.notify(0,note);
You need to make sure all the defaults are overridden by setting
notification.defaults = 0;
Have a look on source below. Working perfectly on S3:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setPriority(Notification.PRIORITY_HIGH)
.setSmallIcon(getNotificationIcon())
.setColor(0xff493C7C)
.setContentTitle(ctx.getString(R.string.app_name))
.setContentText(msg)
.setDefaults(Notification.DEFAULT_SOUND)
.setLights(0xff493C7C, 1000, 1000)
.setStyle(new NotificationCompat.BigTextStyle().bigText(styledMsg));

Categories

Resources