When starting an IntentService to upload a file in the background, I want to show a notification to the user that the upload is in progress by calling showNotification() from inside my service:
private void showNotification() {
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_cloud_upload_black_24dp)
.setWhen(System.currentTimeMillis())
.setContentTitle("Uploading")
.setContentText("Your upload is in progress.")
.setOngoing(true)
.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
Now here's my problem: the notification appears on the lock screen, but not in the status bar when the screen is unlocked. What am I missing?
Deployment target is API level 24, so a missing NotificationChannel should not be the cause.
try it :
PendingIntent pendingIntent = PendingIntent.getActivity(getmContext(), 0, new Intent(getmContext(), MainActivity.class), 0);
android.app.Notification pp = new NotificationCompat.Builder(getmContext())
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentText(getMessage())
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getmContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, pp);
This is my code which is working fine with target SDK 25
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
where NOTIFICATION_ID
public static final int NOTIFICATION_ID = 100;
I feel so dumb. Turns out the notification was displayed all the time, but with a black icon on black background.
Changed the icon colour in the xml from
android:fillColor="#FF000000"
to
android:fillColor="#FFFFFFFF"
and it works like a charm
Related
I'm creating a notification by
mNotifyBuilder.setContentTitle(envelope.getSubjectWithDefault(context.getString(R.string.no_subject)))
.setTicker(sybject)
.setSmallIcon(icon)
.setContentInfo(name)
.setDeleteIntent(PendingIntent.getBroadcast(context, (int) id, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT))
.setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT))
.setAutoCancel(true)
.setNumber(count)
.setGroup(group)
.addAction(reply)
.addAction(cancel, context.getString(R.string.delete), dismiss)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(text))
.setContentText(text);
String noti = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(noti);
mNotificationManager.notify((int) id, notify);
but some phones create for every push their own small icon on the status bar.
How can I found or what I can change in code to fix such notification to work?
This is the code that I have:
NotificationManager notifManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.setBigContentTitle("Title - Notification");
inboxStyle.setSummaryText("TEST");
inboxStyle.addLine("LINE");
Notification noti = new Notification.Builder(getActivity())
.setContentTitle("PSNGR")
.setContentText("PITSTOP BLA").setSmallIcon(R.drawable.notification_icon)
.setStyle(inboxStyle)
.setContentIntent(null).build();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notifManager.notify(0, noti);
But nothing from the InboxStyle notification works. I only get PSNGR and PITSTOP BLA, why is that?
PS: Also tried with BitTextStyle() but also nothing is seen:
For this code:
NotificationManager notifManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity())
.setContentTitle("PSNGR")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText("MATA"))
.setStyle(new NotificationCompat.BigTextStyle().bigText("MATA2"))
.setAutoCancel(true)
.setContentIntent(null);
notifManager.notify(0, notificationBuilder.build());
I only see: "PSNGR"
I used RemoteViews instead of the normal notification:
NotificationManager notifManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getActivity()).setSmallIcon(R.drawable.ic_launcher);
RemoteViews mContentView = new RemoteViews(getActivity().getPackageName(), R.layout.pitstop_notification);
mContentView.setImageViewResource(R.id.image, R.drawable.notification_icon);
mContentView.setTextViewText(R.id.title, "Custom notification");
mContentView.setTextViewText(R.id.text, "This is a custom layout");
mContentView.setTextViewText(R.id.text2, "This is a custom layout2");
mBuilder.setContent(mContentView);
notifManager.notify(0, mBuilder.build());
When I add .setDefaults(DEFAULT_SOUND) or DEFAULT_LIGHTS or DEFAULT_ALL, it shows the heads up notification, but doesnt show the light set with .setLights(0x0FF00FF0, 300, 100). If I remove the .setDefaults it doesn't show the heads up, but shows the lights.
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("http://www.wgn.com"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle("Yi Warning")
.setContentText("Battery is below 20%")
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setVisibility(1)
.setContentIntent(contentIntent)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_SOUND)
.setLights(0x0FF00FF00, 300, 100).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Removed
.setDefaults(Notification.DEFAULT_SOUND)
and added
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
Now everything is working great :D
I'm working on implementation of Pomodoro app for Android Wear.
I want to make similar to standard timer UI/UX, I guess it's implemented using displaying/updating notification with timer as title, so I'm showing notification and updating it periodically from Service:
private void updateNotification() {
Intent stopActionIntent = new Intent(this, PomodoroActivity.class);
stopActionIntent.setAction(PomodoroActivity.ACTION_STOP);
PendingIntent stopActionPendingIntent = PendingIntent.getActivity(this, 0, stopActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action stopAction = new NotificationCompat.Action.Builder(
R.drawable.ic_stop,
getString(R.string.stop_pomodoro),
stopActionPendingIntent)
.build();
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_pomodoro_timer))
.setContentTitle(convertDiffToTimer(System.currentTimeMillis(), timerDeadlineMs))
.setPriority(Notification.PRIORITY_MAX)
.setOngoing(true)
.extend(new NotificationCompat.WearableExtender().addAction(stopAction))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, notification);
}
Unpleasure problem with such solution is blinking of notificaiton.
Any ideas how escape blinking? Or maybe other way to achieve target behaviour?
To update ongoing/existing notification -
Use Same Id of Notification in Builder
Use .setOnlyAlertOnce(true)
NotificationCompat.Builder notificationBuilder;
public void generateNotificationForTimer(String timeInString, boolean isFirstTime) {
if (isFirstTime) {
notificationBuilder = new NotificationCompat.Builder(this)
.setStyle(new NotificationCompat.BigPictureStyle())
.setOnlyAlertOnce(true)
.setContentTitle("Timer Notification Demo")
.setContentText("Time - " + timeInString)
.setSmallIcon(R.drawable.common_signin_btn_icon_dark);
NotificationManagerCompat.from(this).notify(110, notificationBuilder.build());
} else {
notificationBuilder.setContentText(timeInString);
NotificationManagerCompat.from(this).notify(110, notificationBuilder.build());
}
}
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