I created a notification using android.support.v4.app.NotificationCompat.Builder. The notification shows up fine in the notification area.But it doesn't show the default close button on android 4.2.
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setOngoing(boolean)
As per the docuentation,its a type of regular notification which should show the X close button but it doesn't.
Using the following code:
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(iconId);
mBuilder.setContentTitle(titleText);
mBuilder.setContentText(moreinfoText);
mBuilder.setOngoing(false); //to make it regular
PendingIntent pintent =
PendingIntent.getActivity(context,noteId,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pintent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(noteId, mBuilder.build());
Searched on Google but couldn't find anything similar.
Please suggest.
Thanks for your time!
Related
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 am trying to create a simple notification. With this code, the notification appears as an icon in the notification area, and when I open up the notification drawer it is there...
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.midday);
mBuilder.setContentTitle("Midday");
mBuilder.setContentText("Now it is midday");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
I want this notification content to actually show on the top of the screen, not just that the little icon appear on the top bar.
Like this,
I tried searching for this and found that...
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
should help. But it didn't show the notification, only the little icon appears.
If you can help, that would be great.
This is the answer:
Its called a Heads-up Notifications.
This should be enough:
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
However, I have to add this line:
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
Hopefully google will fix this
I am trying to implement local notifications on an android application that I am building. What I want to do is to have a local notification once a week, and when the user presses the notification then a new activity will appear with the actual notification text.
I tried the tutorial below but with no luck:
http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/
the notifications is not repeating every week and when i press it the first time that it appears, then it does not open the new activity.
Anyone who can give me a better tutorial or a good solution?
I don't know exactly how to set up your weekly notifications but i know how to start Activity when user click on Notification and clear the notification list after it:
There is declaration of Notification manager, and notification builder:
NotificationManager notificationManager = (NotificationManager)
Context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
Uri alarmSound = RingtoneManager.getDefaultUri(R.raw.ok);
notificationBuilder.setSound(alarmSound);
//There is declaration which Activity will be opened if user click on notification:
Intent notificationIntent = new Intent(context, Notifications.class);
//clear notification after clicking on it:
notificationBuilder.setAutoCancel(true);
PendingIntent newIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notificationBuilder.setContentIntent(newIntent);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("APP TITLE").setContentText("New notification");
notificationManager.notify(001, notificationBuilder.build());
I hope this will help you!
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.
I've been working on push notifications and I am able to implement it and display it on status bar, the problem I am facing is that I want to display it even if the phone is lock, Under the lock screen where it says ("drag to unlock"), I have seen notifications like that but cant find any example to that.
Example:
Just like when you received a missed call , it will show it under the lock button on your screen.
Code:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon_launcher;
CharSequence tickerText = "MyApplication";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS;;
CharSequence contentTitle = this.title;
CharSequence contentText = this.message;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTICE_ID, notification);
Create Notification using NotificationCompat.Builder
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());
Push Notification on locked Screen
http://www.hongkiat.com/blog/android-lock-screen-notifications/
Create Notification using NotificationCompat.Builder but make sure to put visibility to public like
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder
.setContentTitle("Title")
.setContentText("content")
.setSmallIcon(R.mipmap.ic_launcher)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);//to show content in lock screen
Have you tried creating the alertdialog with a flag? The flag_show_when_locked should do the trick.
Please refer to this thread, you should find a more detailed answer here.
Android Lock Screen Widget
I fixed this by adding this line to notification builder
builder.setOngoing(true);
It will also make notification not cancelable by user, but it solves the problem.
Credits to: Marian Klühspies (link)
The notifications you have seen may actually be widgets placed on a custom widget host lockscreen.
If you look at the android platform source code for InstallWidgetReceiver as late as 4.4.3 here:
https://android.googlesource.com/platform/packages/apps/Launcher3/+/master/src/com/android/launcher3/InstallWidgetReceiver.java
You will see this note by the author:
/**
* We will likely flesh this out later, to handle allow external apps to place widgets, but for now,
* we just want to expose the action around for checking elsewhere.
*/
And you can see that InstallWidgetReceiver.java is in fact not fleshed out by google in the same way as InstallShortCutReceiver.java is. So it seems at least up to 4.4.3 you cant add widgets to the native lock screen in the same way that you can for example add a shortcut to the homescreen using InstallShortCutReceiver.
Unless you build your own lockscreen app as a widget host and the user installs in lieu of the native you may be out of luck using a widget.
Another approach however is to just us an activity that sets getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
This will display your activity whether the screen is locked or not. Dismissing this activity when the screen is locked will display the locked screen.