I want to only display a notification icon, I don't want that notification in the app drawer also. I have an alarm clock app, where I only want to set the icon, but not have something in the drawer, like the default android alarm clock app. My current code is below.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.jolt_action_bar_logo);
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
While it creates a notification icon. It also creates a notification in the drawer.
It's impossible to do what you are wanting to do. The idea of notification icons is so that the user can get any idea of what notifications that the user has yet to act upon. If a notification icon is displayed, the user expects to see a notification of it in the notification shade.
Also, the alarm clock icon you are referring to that the default alarm clock app puts out isn't a notification icon, rather it is an indicator icon to let you know that something is happening (or going to happen) with your device. Examples of indicator icons include the alarm clock icon, the WiFi connectivity icon, the mobile network icon, the NFC icon on some devices, etc. If your app is a alarm clock app, you could tell the Android system to display the alarm icon, as outlined here.
Related
How can I remove all notifications in the notification tray that were sent by my server when the user clicks on one notifiaction?
I have a chatting-app and the user gets a notification for each message (if the app is not in foreground). When the user clicks on one of those notifications the app will be brought to foreground / will be started. After that has happened, I want all other notifications in the notification bar to disappear as well.
No, you dont have to save the ids of all notifications, you can simply call:
NotificationManager nManager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
nManager.cancelAll();
Use the following code to cancel a Notification:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.
I am creating notification from my app on Phone in the below way.
private void launchMicroAppFromWearableNotif() {
int notificationId = 001;
// The below action has been defined in the micro app
Intent i = new Intent("com.microapp.action.PLAY_MUSIC");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent playPendingIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.play_music)
.setContentTitle("Play Music")
.setContentText("Play Music on Wear")
.setContentIntent(playPendingIntent)
.addAction(R.drawable.play_music, getString(R.string.act1), playPendingIntent);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
}
I have created a wearable micro app which has an activity(PlayActivity) with action "com.microapp.action.PLAY_MUSIC" defined in wearable Manifest.
When I take the action from notification on the wearable I expect the activity form the micro app to be launched. But nothing happens. Could someone please help on this. Is it the right way to do it?
Actions that you add to a notification are for the device that the notification was created on, so if you create a notification on your phone, when you click on it on your watch, the action will be sent back to your phone to be executed.
If you want to open your activity on your watch, you cannot achieve that by sending a notification from your phone in the manner you have done. You have a couple of options: for example you can send a message from your phone to your watch and then have the wear side of your app catch that message and put out a "local" notification on the watch; then the actions on that notification are local to your watch and you can open your desired activity via the actions in that notification. If you directly want to open an activity on your watch from your phone (i.e. you don't need a notification to show up on your watch), then you can handle the message that I talked about on your watch and instead of creating a notification there, simply open the desired activity.You may find the WearCompanionLibrary useful in handling some of these cases.
I feel like this should be trivial but I can't seem to make a notification show up on the phone's screen - it only shows up in the status bar at the top.
For an example of what I want to do, here's how Facebook Messenger shows up on the screen when you receive a message.
Whenever I send a notification, all it does is show the little icon in the status bar - even if I set the priority to PRIORITY_MAX. Is there another setting I need to do to make it show on screen instead of just status bar?
The Notification display code:
PendingIntent contentIntent = PendingIntent.getActivity(context, nextId++, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(description)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_stat_notification)
.setLargeIcon(largeIcon)
.setPriority(Notification.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL);
if (android.os.Build.VERSION.SDK_INT >= 21) {
builder.setColor(context.getResources().getColor(R.color.orange_500))
.setVisibility(Notification.VISIBILITY_PUBLIC);
}
Notification notification = builder.build();
notificationManager.notify(id, notification);
All things considered, it's a really good idea to use NotificationCompat.Builder over Notification.Builder, let alone creating a Notification manually. It gives you nice backwards compatibility with graceful degradation (all the way back to API Level 4, otherwise known as "gadzooks, that's old"). AFAIK, it's the only way to get some of the Android Wear stuff going, when used in concert with NotificationManagerCompat. And, in this case, it seems to be happier with the newer Android 5.0+ features.
In this case, setPriority(NotificationCompat.PRIORITY_HIGH) on a NotificationCompat.Builder, used with NotificationManagerCompat, will give you the heads-up notification on Android 5.0+.
Another point, make sure the 'importance' of the notification channel you have set up for your notification is set to NotificationManager.IMPORTANCE_HIGH.
This configures how visually intrusive notifications posted to this channel are and 'high' will allow it to peek. If you have this set to NotificationManager.IMPORTANCE_DEFAULT then it won't!
Bear in mind when configuring a notification channel, once the code has ran on your device, you won't be able to alter this again. So if you need to change the importance you will need to uninstall the app and then re-run and you should see the notification on your screen!
Working on an application where I am receiving notifications in my device.
I have already set the graphic icon for the notifications in my code by using below code.setSmallIcon(drawableicon);
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this);
notifBuilder.setSmallIcon(drawableicon);
notifBuilder.setTicker(ticker);
notifBuilder.setContentTitle(title);
notifBuilder.setContentText(text);
// notifBuilder.setLargeIcon(((BitmapDrawable)getResources().getDrawable(R.drawable.gift_candy_left)).getBitmap());
notifBuilder.setContentIntent(contentIntent);
if(!isAppForground()) {
Uri notificationSound =Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.qubodup_popup);
notifBuilder.setSound(notificationSound);
}
NotificationManager notifManager = (NotificationManager)this.getSystemService(
Context.NOTIFICATION_SERVICE);
notifManager.notify(notifId, notifBuilder.build());
At the instant when device receives notification, in the Status Bar, the notification icon does not fits.
The icon fits well when I pull down the notification panel.
Please suggest me, how to work on the notification icon which I have show on Status Bar at the instant when notification receives on device.
I'm trying to put my notification on top of notification area.
A solution is to set the parameter "when" to my notification object with a future time like:
notification.when = System.currentTimeMills()*2;
The code that I'm using in this:
long timeNotification = System.currentTimeMillis()*2;
Notification notification = new Notification(statusIcon,c.getResources().getString(R.string.app_name),timeNotification);
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notification.when = timeNotification;
notification.priority = Notification.PRIORITY_MAX;
but some apps (like Facebook) are able to put a simple notification with their current time over mine.
If I refresh my notification it remains under these ones.
What parameters I have to set to put my Notification to the top of the notifications area?
You should do this. Other answers seem outdated.
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX);
setPriority(Notification.PRIORITY_MAX) is important. It can also be replaced with any of the following as per requirement.
Different Priority Levels Info:
PRIORITY_MAX --
Use for critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.
PRIORITY_HIGH --
Use primarily for important communication, such as message or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.
PRIORITY_DEFAULT --
Use for all notifications that don't fall into any of the other priorities described here.
PRIORITY_LOW --
Use for notifications that you want the user to be informed about, but that are less urgent. Low-priority notifications tend to show up at the bottom of the list, which makes them a good choice for things like public or undirected social updates: The user has asked to be notified about them, but these notifications should never take precedence over urgent or direct communication.
PRIORITY_MIN --
Use for contextual or background information such as weather information or contextual location information. Minimum-priority notifications do not appear in the status bar. The user discovers them on expanding the notification shade.
For more details check the following link:
http://developer.android.com/design/patterns/notifications.html#correctly_set_and_manage_notification_priority
You can make your notification Ongoing, when it will appear higher then other usual notification. But in this case user would not be able to clear it manually.
In order to do this set flags to your Notification object:
notif.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR
Try setting priority of the notification to high
documentation > Notification Priority
Also check this question may it could help you Pin Notification to top of notification area
Please note that if you want a "heads-up" notification i.e., one that displays over the top of the current user window you must have the following set in your builder:
setDefaults(NotificationCompat.DEFAULT_VIBRATE)
The reference is in the javadoc:
A notification that vibrates is more likely to be presented as a heads-up notification, on some platforms.
Complete example for a heads-up notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE);