I have created a notification using a simple layout with textview and imageview. But after calling notificationManager.notify(...); does not show the notification.
Following is the code i have created
// Create notificationmanager instance
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_LOW;
// Setting the channelid if versioncode is greater than Oreo's
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
notificationManager.createNotificationChannel(mChannel);
}
// Creating remote views for the notifications
RemoteViews notificationLayoutCollapsed = new RemoteViews(context.getPackageName(), R.layout.notification_layout_collapsed);
RemoteViews notificationLayoutExpanded = new RemoteViews(context.getPackageName(), R.layout.notification_layout_expanded);
// Passing the intent
Intent notificationIntent = new Intent(context, HomeActivity.class);-
// setting the flags
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// Style MOST LIKELY ISSUE IS HERE
NotificationCompat.Style style = new androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle();
NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.app_name))
.setContent(notificationLayoutExpanded)
.setCustomContentView(notificationLayoutCollapsed)
.setCustomBigContentView(notificationLayoutExpanded)
.setStyle(style)
.setAutoCancel(false)
.setContentIntent(pendingIntent);
notificationManager.notify(NOTIFICATION_ID, mBuilder1.build());
I have also tried using the style = new NotificationCompat.DecoratedCustomViewStyle();
But when i set the style as BigText or BigPicture and do not set the customcontentview i do see the notification.
Edit
I have used both v4.NotificationCompat, androidx.NotificationCompat
Problem is in my code, I think some step is missing. Pls help.
Related
I write this code to show a notification in android. But this notification is only shown in the status bar. I want when a message received show notifications on the top screen like telegram app:
enter image description here
My code:
NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String offerChannelId = "offerChannelId";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String offerChannelName = "offerChannelName";
String offerChannelDescription = "offerChannelDescription";
int offerChannelImportance = NotificationManager.IMPORTANCE_HIGH;
#SuppressLint("WrongConstant") NotificationChannel notifChannel = new NotificationChannel(offerChannelId, offerChannelName, offerChannelImportance);
notifChannel.setDescription(offerChannelDescription);
mNotifyManager.createNotificationChannel(notifChannel);
}
NotificationCompat.Builder sNotifBuilder = new NotificationCompat.Builder(getBaseContext(), offerChannelId);
sNotifBuilder.setSmallIcon(R.drawable.ic_notifications)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_notifications))
.setColor(getResources().getColor(R.color.baseColor_yellow))
.setContentTitle("title")
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX);
mNotifyManager.notify(1, sNotifBuilder.build());
How I do it?
Min SDK is 21.
Thanks in advance.
I found a solution for you. Hope it may help you.
//build notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Ping Notification")
.setContentText("Tomorrow will be your birthday.")
.setDefaults(Notification.DEFAULT_ALL) // must requires VIBRATE permission
.setPriority(NotificationCompat.PRIORITY_HIGH) //must give priority to High, Max which will considered as heads-up notification
.addAction(R.drawable.dismiss, getString(R.string.dismiss), piDismiss)
.addAction(R.drawable.snooze, getString(R.string.snooze), piSnooze);
//set intents and pending intents to call service on click of "dismiss" action button of notification
Intent dismissIntent = new Intent(this, MyService.class);
dismissIntent.setAction(ACTION_DISMISS);
PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0);
//set intents and pending intents to call service on click of "snooze" action button of notification
Intent snoozeIntent = new Intent(this, MyService.class);
snoozeIntent.setAction(ACTION_SNOOZE);
PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);
// Gets an instance of the NotificationManager service
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* Notification for oreo*/
String channelId = "channel-01";
String channelName = "Demo";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
//to post your notification to the notification bar with a id. If a notification with same id already exists, it will get replaced with updated information.
notificationManager.notify(0, builder.build());
Minimum SDK is Lolipop.
Below mentioned code is working for all devices below android O verison. For android O,
addAction() method is not working i.e. button click is not working in android O.
Any help would be appreciated.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mediaPlayerReceiver = new Intent("com.consult.news.receiver.ACTION_PLAY");
mediaPlayerReceiver.putExtra("NewsArticle", news);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mediaPlayerReceiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent dismissNotification = new Intent("com.consult.news.receiver.DISMISS");
dismissNotification.putExtra("Notification_ID", 1);
PendingIntent dismissNotificationIntent = PendingIntent.getBroadcast(context, 0, dismissNotification, PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
String CHANNEL_NAME = "my Channel Name";
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "my_channel_01")
.setSmallIcon(R.drawable.ic_notification_white)
.setColor(ContextCompat.getColor(context, R.color.accent))
.setContentTitle(context.getString(R.string.Consult_Univadis_Title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(news))
.addAction(isPlaying ? R.drawable.ic_notification_white : R.drawable.ic_notification_white, isPlaying ? "Play" : "Pause", pendingIntent)
.addAction(R.drawable.ic_notification_white, "Close", dismissNotificationIntent)
.setOngoing(true)
.setAutoCancel(false);
notificationManager.notify(1, builder.build());
I ran into the same, in Android Oreo you need to make it a explicit Intent (is not enough with putting the receiver on the manifest, in fact, it won't pay attention to that), so when you make the intent, make it explicit using the setClass method:
Intent mediaPlayerReceiver = new Intent("com.consult.news.receiver.ACTION_PLAY");
mediaPlayerReceiver.putExtra("NewsArticle", news);
mediaPlayerReceiver.setClass(this, YourReceiver.class);
Where "this" is the Context and YourReceiver, is the Receiver class that you are expecting to listen to the action.
You will have to do the same for the dismissNotification intent
Let me know if this worked for you.
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());
How to create custom default notification? I am using Remote view to create the notification. but its not what was expect. Below is the image attched. I want to create the notification as that, with two button at bottom(like The Big Meeting is defined.
Any tutorial will be really helpful.
Below is code snippet what I have written.
Intent snoozeIntent = new Intent(LockableActivity.INTENT_SNOOZE);
PendingIntent pendingSnoozeIntent = PendingIntent.getBroadcast(context, 0, snoozeIntent, 0);
Intent gotItIntent = new Intent(LockableActivity.INTENT_GOT_IT);
PendingIntent pendingGotItIntent = PendingIntent.getBroadcast(context, 0, gotItIntent, 0);
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.view_notification);
remoteView.setTextViewText(R.id.tv_title, entity.getClientName() + " appointment is late");
remoteView.setTextColor(R.id.tv_title, context.getResources().getColor(android.R.color.black));
remoteView.setOnClickPendingIntent(R.id.btn_snooze, pendingSnoozeIntent);
remoteView.setOnClickPendingIntent(R.id.btn_got_it, pendingGotItIntent);
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(entity.getClientName() + " appointment is late")
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.notification_content)))
.setContent(remoteView);
But here I have my xml. Is there other way by which I could DD THE BUTTONs at bottom.
You need to set your remote view to the bigContentView field in the notification
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(entity.getClientName() + " appointment is late")
.setAutoCancel(true).build();
notification.bigContentView = remoteView;
mNotificationManager .notify(0,notification);
I've build a push notification using Big Picture Style as show here.
Is it possible to mix Big picture Style and Big Text Style as shown in the attached photo? How do I do it?
You should be able to do it this way:
Notification notif = new Notification.Builder(context)
.setContentTitle("Title")
.setContentText("content")
.setSmallIcon(R.drawable.ic_small)
.setLargeIcon(bitmap)
.setStyle(new Notification.BigPictureStyle()
.bigPicture(bigBitmap)
.setBigContentTitle("big title"))
.build();
Source
View More... text in your norification is subtext.
So while using bigpicturestyle notification you need to set
bigPicStyle.setSummaryText(mContent);
or
mBuilder.setSubText(mSubText);
Setting both at same time doesn't work.
Example of how to create a BigPictureStyle Notification:
int NOTIFICATION_ID = 1;
String ns = Context.NOTIFICATION_SERVICE;
//Get the bitmap to show in notification bar
Bitmap bitmap_image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap big_bitmap_image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle()
.bigPicture(big_bitmap_image)
.setSummaryText(getResources().getString(R.string.content));
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
nb.setContentTitle("Notification title"))
.setContentText("Notification Content")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(bitmap_image)
.setTicker("Notification ticker!")
//API Level min 16 is required
.setStyle(style)
.build();
Intent notificationIntent = new Intent(this, MainActivity2.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
TaskStackBuilder TSB = TaskStackBuilder.create(this);
TSB.addParentStack(MainActivity.class);
TSB.addNextIntent(notificationIntent);
PendingIntent resultPendingIntent = TSB.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
nb.setContentIntent(resultPendingIntent);
nb.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, nb.build());
this is the complete code:
https://github.com/Jorgesys/Android-Notifications