I m trying to create a custom notification using RemoteView. It is showing properly in Nougat and above OS version devices but has an issue with marshmallow and below OS version devices.
Here is default Big icon (Left side inside the Red circle) and default time (In Right side red circle) is not hiding after Remote view.
Please suggest me how I can come out of this problem.
private void sendNotification() {
Long id = System.currentTimeMillis();
int notifyId = id.intValue();
RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.view_expanded_notification);
expandedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
expandedView.setTextViewText(R.id.notification_message, mEditText.getText());
// adding action to left button
Intent leftIntent = new Intent(this, NotificationIntentService.class);
leftIntent.setAction("left");
expandedView.setOnClickPendingIntent(R.id.left_button, PendingIntent.getService(this, 0, leftIntent, PendingIntent.FLAG_UPDATE_CURRENT));
// adding action to right button
Intent rightIntent = new Intent(this, NotificationIntentService.class);
rightIntent.setAction("right");
expandedView.setOnClickPendingIntent(R.id.right_button, PendingIntent.getService(this, 1, rightIntent, PendingIntent.FLAG_UPDATE_CURRENT));
RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.view_collapsed_notification);
collapsedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "imp")
// these are the three things a NotificationCompat.Builder object requires at a minimum
.setSmallIcon(R.drawable.ic_pawprint)
.setContentTitle(NOTIFICATION_TITLE)
.setContentText(CONTENT_TEXT)
// notification will be dismissed when tapped
.setAutoCancel(true)
// tapping notification will open MainActivity
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
// setting the custom collapsed and expanded views
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
// setting style to DecoratedCustomViewStyle() is necessary for custom views to display
.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
// retrieves android.app.NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("imp", "Test", importance);
channel.setDescription("For testing");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
notificationManager.notify(notifyId, builder.build());
} else {
NotificationManager notificationManager = (android.app.NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
This is properly shown in Nougat and above devices.
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, id);
builder.setSmallIcon(R.drawable.icon_notification)
.setAutoCancel(true)
.setCustomBigContentView(remoteExpandedViews)
.setCustomContentView(remoteCollapsedViews)
.setContentIntent(pendingIntent)
**.setPriority(Notification.PRIORITY_MAX)**
.setVibrate(new long[]{0});
set Priority to PRIORITY_MAX using .setPriority(Notification.PRIORITY_MAX) in builder
Related
I have an calendar app where the info is downloaded online and there is a recyclerView of events and I want to notify the user when there is a calendar event for today, and notify with the event name or names. I have done some googling but haven't been able to figure this out.
In my MainActivity.java this is the method I created as a test from a page online
private void addNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_check);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, mBuilder.build());
}
But when I call this method nothing is happening, what am I doing wrong?
I think you are missing the channel of the notification
I have a different solution, try this one (I checked it on my phone)
calling the function:
showSmallNotification(R.drawable.ic_launcher_background, "title","message");
Function code
private void showSmallNotification( int icon, String title, String message){
String CHANNEL_ID = "Channel_01";
String CHANNEL_NAME = "Notification";
// I removed one of the semi-colons in the next line of code
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// I would suggest that you use IMPORTANCE_DEFAULT instead of IMPORTANCE_HIGH
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setLightColor(Color.BLUE);
channel.enableLights(true);
//channel.canShowBadge();
// Did you mean to set the property to enable Show Badge?
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
}
Intent mainIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setLights(Color.BLUE, 3000, 3000)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(inboxStyle)
.setContentText(message);
// Don't forget to set the ChannelID!!
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
notificationBuilder.setChannelId(CHANNEL_ID);
}
notificationManager.notify(CHANNEL_ID, 1, notificationBuilder.build());
}
Edit
In order to check if there are any events every day you will need to use AlarmManager as from it's name you can schedule an event every day
You can see a solution (or at least it will give a direction) once you did that I think the next step would be to send the intent to a service which will check if there are any events available in current day. If there are any just show the notification/s
alarm manager link
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(context, AlertDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // added new line
intent.putExtra("STARTED_BY_RECEIVER", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
String CHANNEL_ID = "channel id";// The id of the channel.
CharSequence name = "Channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
Notification notification = new Notification.Builder(context)
.setContentIntent(pendingIntent)
.setContentText("Some Text")
.setContentTitle("Notification")
.setAutoCancel(true)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_notification_praying)
.setChannelId(CHANNEL_ID)
.build();
notificationManager.createNotificationChannel(mChannel);
notificationManager.notify(100 , notification);
} else {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(context, AlertDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // added new line
intent.putExtra("STARTED_BY_RECEIVER", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification_praying)
.setContentIntent(pendingIntent)
.setContentText("Some Text")
.setContentTitle("Notification")
.setAutoCancel(true)
.setOngoing(true);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
notificationManager.notify(100, builder.build());
}
}
I've made an application which show notification on time that user chose in main activity and Notification should disappear only when user tap on notification otherwise not..and it was working fine unless I've updated my phone to android Oreo (8.0) now notification show at time but auto disappear after sometime.. How to make notification not auto disappear (in android Oreo) but only when user tap on it?
alright I figure this out.. it was battery optimize option which was killing my application so that's why notification was disappearing.. I removed my application from auto optimize now it's working fine.
I am currently working on an android app. I need to notify a user that they have arrived at a place in the normal notification and when the notification is expanded it shows extra info. The usual :)
I am using 2 different remoteViews, one to set the contentView and one to set the bigContentView.
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// build notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(notificationPendingIntent)
.setColor(Color.CYAN)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true)
.setContentTitle("Content Title")
.setContentText("Content Text");
int NOTIFICATION_ID = 1;
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.remoteview_notification);
rv.setImageViewResource(R.id.remoteview_notification_icon, R.mipmap.ic_launcher);
rv.setTextViewText(R.id.remoteview_notification_headline, "Headline");
rv.setTextViewText(R.id.remoteview_notification_short_message, notificationContent.getHeading());
Notification notification = mBuilder.build();
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification.contentView = rv; //Alternative to .setContent(rv);
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
RemoteViews rvBig = new RemoteViews(context.getPackageName(), R.layout.remoteview_big_notification);
rvBig.setImageViewResource(R.id.remoteview_big_notification_icon, R.mipmap.ic_launcher);
rvBig.setTextViewText(R.id.remoteview_big_notification_headline, notificationContent.getHeading());
rvBig.setTextViewText(R.id.remoteview_big_notification_full_message, notificationContent.getBodyText());
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = rvBig;
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
The problem is my expanded notification does not show up when I drag down? Any help will be appreciated. It feels like it is just something small that is not yet set/ set correctly?
I believe that my intentId might not be unique throughout my app. I have seen people use the current system to ensure uniqueness.
NOTIFICATION_ID = system.currenttimemillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationManager.notify(NOTIFICATION_ID, builder.build());
I'm not sure what I'm doing wrong, the notification is the way I like it but I cannot get it to make the watch vibrate, and it shows as minimized. the effect I want to achieve should look like hangouts notification, which vibrate and go fullscreen. here is the code I'm using (on the watch):
Intent actionIntent = new Intent(this, ConvActivity.class);
actionIntent.putExtra("num",num);
PendingIntent actionPendingIntent =
PendingIntent.getActivity(this, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the action
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_launcher,"Reply"
, actionPendingIntent)
.build();
NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(body).setBigContentTitle(name);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(name)
.setContentText(body)
//.setContentIntent(viewPendingIntent)
//.addAction(R.drawable.ic_map,
// getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle).setAutoCancel(true)
.addAction(R.drawable.ic_launcher,"Reply"
, actionPendingIntent);
if(pic != null)
notificationBuilder.setLargeIcon(BitmapFactory.decodeByteArray(pic,0,pic.length));
//.extend(new NotificationCompat.WearableExtender().addAction(action)) ;
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Issue the notification with notification manager.
notificationManager.notify(0, notificationBuilder.build());
Only notifications that vibrate/sound will vibrate an Android Wear device and show more text.
You should consider using the NotificationCompat.Builder.setDefaults() method:
// Light, sound, and vibrate
builder.setDefaults(Notification.DEFAULT_ALL);
// Just sound
builder.setDefaults(Notification.DEFAULT_SOUND);
// Just vibrate
builder.setDefaults(Notification.DEFAULT_VIBRATE);
I need to implement expand and collapse notification in status bar for android 4.0 and above version. I have search on google for this but didn't getting any code solution for implementation does anybody have I idea how to implement this
Thank You in advance
An expandable Notification is a special case of a Notification Big View. If the Big View is not at the top of the notification drawer, it it shown 'closed' and is expandable by swipe. Quote from Android Developers:
A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications are available starting with Android 4.1.
The Big View Notification can be created as follows:
Notification notification = new Notification.BigTextStyle(builder)
.bigText(myText).build();
or
Notification notification = new Notification.BigPictureStyle(builder)
.bigPicture(
BitmapFactory.decodeResource(getResources(),
R.drawable.my_picture)).build();
Here is a tutorial.
Notification noti = new Notification.Builder()
... // The same notification properties as the others
.setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap))
.build();
You change
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
along with the announcement OK !!!
notification = new NotificationCompat.Builder(context)
Here is an example:
Expandable Notifications and Notifications Groups
You can set Code
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
// bigStyle.bigText((CharSequence) context);
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(th_title)
.setContentText(th_alert)
.setAutoCancel(true)
// .setStyle(new Notification.BigTextStyle().bigText(th_alert) ตัวเก่า
// .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
.setContentIntent(pendingIntent)
.setNumber(++numMessages)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);
or
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
// .setContentTitle(notification.getTitle())
.setContentTitle(getResources().getText(R.string.app_name))
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notification.getBody()))
.setContentInfo(notification.getTitle())
.setLargeIcon(icon)
.setColor(Color.RED)
.setSmallIcon(R.drawable.logo);
try {
String picture_url = data.get("picture_url");
if (picture_url != null && !"".equals(picture_url)) {
URL url = new URL(picture_url);
Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
notificationBuilder.setStyle(
new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
);
}
} catch (IOException e) {
e.printStackTrace();
}
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
notificationBuilder.setLights(Color.YELLOW, 1000, 300);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
27/07/2021 :
You should can read detail this : Expandable Notifications and Notifications Groups
I Couldn't able to set new instance of new NotificationCompat.BigTextStyle() in .setStyle() method of Notification. So I have used the below one, new instance of new Notification.BigTextStyle() in .setStyle().
Notification builder =new Notification.Builder(this)
.setSmallIcon(Notification_icons[icon])
.setContentTitle(title)
.setContentText(description)
.setChannelId(channelID_Default)
.setOngoing(true)
.setStyle(new Notification.BigTextStyle()
.bigText(description))
.build();
There is a method for this function you can show a small icon when notification collapsed and show a large one when a notification expanded.
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.notification)
var notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.new_post)
.setContentTitle(imageTitle)
.setContentText(imageDescription)
.setLargeIcon(bitmap)
.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null))
.build()
(2021) Stumbled upon this question when I was having issues with expanding my notification which had a larger text.
Solved by referring to the Android docs on Expandable Notifications: Create an Expandable Notification
Nimisha V's answer shows how to expand a large image on a notification. The below code is used for expanding large text on a notification.
var notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle(notification_title)
.setContentText(notification_message)
.setStyle(NotificationCompat.BigTextStyle()
.bigText(notification_message))
.build()
We can't create expandable notification in below android 4.1 versions.
But Instead of this we can do that we can stacked the notifications and then we can set a pending intent to our pretty custom activity which shows all notification in list.
User will happy to see this :)