In some device, I am getting a blank(white) notification like attached screenshot. And in some devices, it's working fine. Please help me to resolve this issue.
Intent intent = new Intent(ctx, NotificationDetailActivity.class);
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setTicker(getResources().getString(R.string.app_name));
// Sets the small icon for the ticker
builder.setSmallIcon(getNotificationIcon());
builder.setLargeIcon(result);
builder.setColor(getResources().getColor(R.color.colorPrimary));
builder.setContentTitle(title);
builder.setContentText(messageBody);
builder.setSound(defaultSoundUri);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
RemoteViews expandedView =
new RemoteViews(ctx.getPackageName(), R.layout.custom_notification);
if (Build.VERSION.SDK_INT >= 16)
{
// Inflate and set the layout for the expanded notification view
expandedView.setImageViewBitmap(R.id.imgBigImage, result);
notification.bigContentView = expandedView;
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Integer.parseInt(id), notification);
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(getNotificationIcon())
.setLargeIcon(result)
.setColor(getResources().getColor(R.color.colorPrimary))
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(id), notificationBuilder.build());
}
Try this way to setBigContentView to NotificationCompat.Builder ,check if it works :-
Intent intent = new Intent(ctx, NotificationDetailActivity.class);
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Notification notification = builder.build();
RemoteViews expandedView =
new RemoteViews(ctx.getPackageName(), R.layout.custom_notification);
if (Build.VERSION.SDK_INT >= 16)
{
builder.setTicker(getResources().getString(R.string.app_name));
// Sets the small icon for the ticker
builder.setSmallIcon(getNotificationIcon());
builder.setLargeIcon(result);
builder.setColor(getResources().getColor(R.color.colorPrimary));
builder.setContentTitle(title);
builder.setContentText(messageBody);
builder.setSound(defaultSoundUri);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
// Inflate and set the layout for the expanded notification view
expandedView.setImageViewBitmap(R.id.imgBigImage, result);
// notification.bigContentView = expandedView;
builder.setCustomBigContentView(expandedView);
} else {
builder.setSmallIcon(getNotificationIcon());
builder.setLargeIcon(result);
builder.setColor(getResources().getColor(R.color.colorPrimary));
builder.setContentTitle(title);
builder.setContentText(messageBody);
builder.setAutoCancel(true);
builder.setSound(defaultSoundUri);
builder.setContentIntent(pendingIntent);
}
nm.notify(Integer.parseInt(id), builder.build());
Related
I'm added Notification code in my android service.
Tried
Notifications Code in Android
Log.e("TAG","In 2");
Intent viewIntent = new Intent(getApplicationContext(), Restarter.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, viewIntent , PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle(getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
notificationBuilder.setStyle(inboxStyle);
inboxStyle.setBigContentTitle("sdjshfjnds");
inboxStyle.addLine("sdjjsdfn");
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int NOTIFICATION_ID = 100;
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
I m getting log message but not notification.
This is the old answer, it's 2015 year. Now it's not actual. Please check the actual documentation: https://developer.android.com/training/notify-user/build-notification
I use this code to show notifications users of my app.
On some devices, this code works perfectly, but some devices do not show a notification at all.
Is someone know what is the problem?
private void sendNotification(String title, String messageBody, Map<String, String> allData) {
Intent intent = new Intent(this, ReferralPage.class);
intent.putExtra("title", title);
intent.putExtra("text", messageBody);
String tid = allData.get("tid");
intent.putExtra("tid", tid);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
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, "CH_"+tid)
.setSmallIcon(com.escodes.R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long time = new Date().getTime();
String tmpStr = String.valueOf(time);
String last4Str = tmpStr.substring(tmpStr.length() - 5);
int notificationId = Integer.valueOf(last4Str);
notificationManager.notify(notificationId, notificationBuilder.build());
}
You need to add a notification channel for API 26 and above, like this:
if (Build.VERSION.SDK_INT >= 26) {
String CHANNEL_ID = "ID_OF_THE_CHANNEL";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"name_of_the_channel",
NotificationManager.IMPORTANCE_DEFAULT);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setContentTitle("title")
.setContentText("text")
...
...
.build();
}
I am using the below code to show the notification, but nothing shows up.
I tried using the notification channel also. still it is not working.
I am testing in an Oreo device.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), "notify_001");
Intent ii = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, ii, 0);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText("sdsd");
bigText.setBigContentTitle("Today's Bible Verse");
bigText.setSummaryText("Text in detail");
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
mBuilder.setContentTitle("Your Title");
mBuilder.setContentText("Your text");
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setLargeIcon(null);
mBuilder .setSubText("");
mBuilder.setWhen(System.currentTimeMillis());
mBuilder.setStyle(bigText);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("notify_001",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(001, mBuilder.build());
I am testing in an Oreo device.
As I've searched how to reveal heads-up, I found a solution that is to set priority high but it still doesn't show heads-ups. I've got a notfication massenger service. Its function is as below. Is there anything I missed in settings?
Heading
mycode:
private void sendNotification(String body, String title) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
remoteViews.setTextViewText(R.id.title, "Firebase");
remoteViews.setTextViewText(R.id.body, body);
remoteViews.setImageViewResource(R.id.icon, R.mipmap.ic_launcher);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{intent}, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.mipmap.ic_launcher)
.setCustomContentView(remoteViews)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle("Firebase Cloud Messaging")
.setContentText(body)
.setCustomHeadsUpContentView(remoteViews)
.setAutoCancel(false)
.setDefaults(Notification.DEFAULT_ALL)
.setTicker(body)
.setSound(notificationSound)
.setFullScreenIntent(pendingIntent, true)
.setContentIntent(pendingIntent);
notificationManager.notify(0, notifBuilder.build());
}
Try replacing .setContentIntent(pendingIntent) with .setFullScreenIntent(pendingIntent, true) in your Notification Builder.
I have search and found solution but that solution is not showing multi line push notification.
My code is below,
int id = (int) System.currentTimeMillis();
Intent intent = new Intent(this, Activity.class);
Bundle bundle = new Bundle();
bundle.putString("id", u_id);
intent.putExtras(bundle);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this);
Notification notification = mBuilder.setSmallIcon(getNotificationIcon()).setTicker(getResources().getString(R.string.app_name)).setWhen(0)
.setAutoCancel(true)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentIntent(resultPendingIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(BitmapFactory.decodeResource(getResources(), getNotificationIcon()))
.setContentText(messageBody).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notification);
How can I show multi line text in push notification?
Intent intent = new Intent(this, target.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Bitmap image= BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.youricon)
.setContentTitle("Title")
.setLargeIcon(image)
.setContentText("Message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).setSummaryText("multiline text"));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(m/* ID of notification */, notificationBuilder.build());