I am using the following code to show a notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, MainActivity.class);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
According to the docs/tutorial.
You can set an explicit value with setWhen();
if you don't it defaults to the time that the system received the notification.
Problem is I am not getting the defalut time in the notification.
--------------------WORK AROUND---------------------
Get the current time using System.currentTimeMillis() and then pass it to when()
long curr_time = System.currentTimeMillis();
mBuilder.setWhen(curr_time);
Let me know if this could be solved without a work around. :)
This was a bug in early versions of ICS; the timestamps should appear again as of version 4.0.3 (fixed here).
Related
Unfortunately another question about my startForegroundService notification... I searched, really, I did:
I have a foreground service that is running perfectly. I would like to add a couple of actions to this notification. For one, make it so when the user clicks the notification they are sent to MainActivity as well as adding a "Quit" addAction.
Here is the snippet I am using to create the notification:
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String channelId = getNotificationChannel(notificationManager);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_notif_icon)
.setContentTitle("My app")
.setContentText("Background service is running...")
.setContentIntent(pendingIntent)
.build();
startForeground(13365, notification);
Using the above a notification shows up just fine, but click on it results in nothing. I also tried using addAction, also nothing. I am aware the syntax is a little bit different (....Action.Builder) when adding an addAction.
I am creating my notification in the onCreate handler of the foreground service. Running on SDK 26.
Can startForeground notifications have setContentIntent / addAction attached to them?
Thanks!
Solved : I had the notification replaced elsewhere in my application and was not adding the intents there.
Doh!
I'm making an android application, and I want to receive notifications when the application is closed, how can I do that? I've read also other posts where people ask it but i can't understand how to make it work, could you please try to explain this better?
I've also tried to make a countdownTimer and make the application do something when the timer reaches the 0 but it didn't work, 'cause obviusly when I close the application, I close the timer too.
use this code to make in app notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
I want to show notification at defined time. I use "setWhen()". But any argument of setWhen() is ignoring and notification always showing instantly. What I did wrong? My code:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setWhen(???)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Test")
.setContentText("Test");
Intent resultIntent = new Intent(this, MainActivity.class);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
That's not the purpose of setWhen. It's only a UI thing, if setShowWhen is enabled this timestamp is shown at the notification.
For your purpose, I would suggest the android AlarmManager. An example can be found here
setWhen() is used to show the time on the notification itself, not when the notification is shown.
The time on the top right of the notification is what setWhen() controls:
For showing the notification at a particular time, you can use the AlarmManager class. This has code to do the same.
I'm trying to show a notification that is removed when the user taps on it.
I'm using the NotificationCompat class to build my notification and I call setAutoCancel(true) on my builder. This is the piece of code:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content");
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
The notification is correctly added but when I tap on it nothing happens! What am I doing wrong?
Using setContentIntent should solve your problem:
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
In your example:
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content")
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.
I uploaded a demo to github.
I know an answer has already been accepted, but I had the same problem with a different solution so I will share it here.
For me, I was using the same NotificationCompat.Builder object to create a notification which called setOngoing(true). This was for an upload progress notification which should not be removed while working.
Anyways, after the task was complete, I called setAutoCancel(true) but the notification was still not swiping away. What I had to do also was call setOngoing(false).
It seems pretty obvious now, but it might save someone else some time in the future.
I have created a notification with NotificationCompat.Builder but i don't know how to show it in status bar. I tried using
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mId, mBuilder.build());
but it doesn't work on 2.2 so i need another way...
this is my notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification)
.setContentTitle("Message")
.setContentText("Notification");
but it doesn't work on 2.2
Yes it does.
This sample application shows an IntentService downloading a file and displaying a Notification using NotificationCompat.Builder. It uses code pretty much like yours for actually raising the Notification:
NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID, b.build());
This code works fine on Android 2.2, as I just tried it (again) on an emulator, and the Notification displays as expected.