BigContentView does not seem to be updated - android

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());

Related

How to send notifications depending on date

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

Android Custom Notification with RemoteView is not showing properly blow Nougat OS

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

Notification Bar in android

I am making an application that uses notification in it but when the notification is about to appear this message is appearing continuously till i turn of the AVD "Unfortunately , System UI has stopped"
Intent viewIntent = new Intent(getBaseContext(), WorkshopActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.drawable.web)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText("Scheduled")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent).setTicker("Compass");
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
notificationBuilder.setStyle(inboxStyle);
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int NOTIFICATION_ID = 100;
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
can you please replace getBaseContext() with 'this' or at least getApplicationContext() because BaseContent has a very rare & spesific usage.
and
.setContentIntent(pendingIntent).setTicker("Compass");
after this can you build the notificationBuilder,
.setContentIntent(pendingIntent).setTicker("Compass").build();
maybe it needs to build before adding style and addign it to manager.
lastly,
notificationManager.notify(NOTIFICATION_ID, notificationBuilder);

Can't display more text in a notification

I am trying to show a notification in the title bar with a long text.
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID, notificationIntent,
PendingIntent.FLAG_ONE_SHOT);
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.icon_push).setTicker(alert)
.setContentTitle(title).setContentText(alert)
.setWhen(System.currentTimeMillis()).setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(title));
}
Notification n = builder.build();
nm.notify(id, n);
But the builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(title));
The setStyle seems to do nothing, I am testing it on android 4.1
You should remove this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
because this is compatible, it's automatically set right.
And this is 100% working code:
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("titletitletitletitletitletitletitletitletitletitletitletitle").setContentText("contentcontentcontentcontentcontentcontentcontent")
.setWhen(System.currentTimeMillis()).setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("bigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbig"));
Notification notification = builder.build();
notificationManager.notify(1, notification);
If your variable names are correct and your variable named title is only the title then your problem is that you are using bigText(title) instead of bigText(aReallyBigText);
use:Notification.Builder(context).setFullScreenIntent(pendingIntent, true),
manual to make the notification full screen

Android remove notification(created by mNotifyBuilder) on click

i found many answer to this, but none help :(
I have this code:
private static void generateNotification(Context context, String message) {
int icon = R.drawable.icon;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notifyID = 96;
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
.setContentTitle("MyApp")
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSmallIcon(icon);
Notification notification = mNotifyBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(notifyID, notification);
}
But if i click on notification nothing happen and it is still there. In documentation is, that i need to use:
.setAutoCancel(true)
Some one have similar problem and somebody tells him to use:
notification.flags |= Notification.FLAG_AUTO_CANCEL;
I use both, but no result :(
Thank you very much for answers. :)
I think that if you don't use an Intent with your notification, the only way to dismiss its to swipe it.
Otherwise you can use an Intent to open your activity and that will actually clear the notification:
Intent showIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);
And to add it to the notification:
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
.setContentTitle("MyApp")
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setSmallIcon(icon);
Hope this helps!
The user can dismiss all notification or if you set your notification to auto-cancel it is also removed once the user selects it.
You can also call the cancel() for a specific notification ID on the NotificationManager. The cancelAll() method call removes all of the notifications you previously issued.
Example:
mNotificationManager.cancel(notifyId);
just use this line inside button onclick event-
mNotificationManager.cancel(notifyId);//id is that u used for notification
just add
Intent showIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);
myNotification = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Text")
.setTicker("notificatio")
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
add empty pending intent then on click on notification, notification will remove. it works for me.
I generally use:
mNotificationManager.cancelAll();

Categories

Resources