My old Nokia phone was flashing hardware buttons when I missed a call. So I was able to understand that I have missed a call just by looking to phone. With my new Android phone I have to reach my phone and wake the screen to see if I have missed a call.
I have searched Android market but could not find exact application to solve my problem. So I have decided to write it. The question is how can I turn on and off back lid of hardware buttons of a android phone?
I have googled it but could not find a clean answer.
Thanks in advance.
Android does have notifications for that purpose, the backlight is not thought to be controlled through the API (you could do it on rooted devices but thats another story).
Personally, I dedinitely do get notifications for missed calls, and my notification LED blinks. However, you can implement your own notifications:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// create a new notification
CharSequence tickerText = "Missed call";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
// control how the notification led should behave
notification.ledARGB = 0xff00ff00;
// blink for 300ms every 1s
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
// usually you also want to create a PendingIntent and attach it
// with notification.setLatestEventInfo
// finally, post the notification to the notification manager
notificationManager.notify(HELLO_ID, notification);
There are many other options for notifications, like vibration or FLAG_AUTO_CANCEL, but they are documented very well ;-)
On a rooted device, you could execute the following to control the backlight (however, I would recommend sticking to the intended way, which are notifications):
su
echo 25 > /sys/class/leds/button-backlight-portrait/currents
echo 25 > /sys/class/leds/button-backlight-landscape/currents
where 25 would be the brightness. But admittetly, I don't know for sure if this would even work on all devices.
Related
I am using the following code to show my notification:
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context,
context.getString(R.string.notification_general_id))
.setSmallIcon(R.drawable.iit)
.setContentTitle("something")
.setContentText(notification)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
// https://stackoverflow.com/a/28251192/2287994
long time = new Date().getTime();
String tmpStr = String.valueOf(time);
String last4Str = tmpStr.substring(tmpStr.length() - 5);
int notificationId = Integer.parseInt(last4Str);
Log.d(TAG, "notificationId " + notificationId);
notificationManager.notify(notificationId, builder.build());
I create notification channel using the following code:
// for showing general notifications
NotificationChannel generalNotificationsChannel = new NotificationChannel(
getString(R.string.notification_general_id),
getString(R.string.notification_general_name),
NotificationManager.IMPORTANCE_HIGH
);
nearbyAnchorsChannel.setDescription(getString(R.string.notification_general_desc));
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
On Google Pixel 3a, the notification only vibrates and shows up as a blip on the top left. However, on the OnePlus 6, it shows up as a proper heads-up notification that we can immediately swipe or tap on. I tried looking through the settings of my Pixel 3a (it is Android 12) but I cannot find any option that I can change to enable a heads-up display of notifications. Tbh, I am not even sure if there is something wrong with my code or the phone I am testing it on. Is it because of my OnePlus 6's Android version (it is Android 11)? Or is it due to the code that I have written? If it is due to the former then can someone please explain to me how I can change settings on my Pixel 3a to show a proper swipeable heads-up notification?
My bad, when you change channel importance, you need to uninstall the app and install it again. That's why it worked on the OnePlus 6 (because I installed it after changing the notification importance) and not on the Google Pixel 3a (because I was still working with the same install)
How can I send a notification with sound and vibration when phone is in do not disturb mode on Android Devices.
I use the following code, and it is working when my application is currently in foreground.
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
resultIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText("You've received new message."))
.setContentText("You've received new message.");
// FOR SILENT MODE
AudioManager am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
// For Normal mode
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
// Set Vibrate, Sound and Light
int defaults = 0;
defaults = defaults | Notification.DEFAULT_LIGHTS;
// defaults = defaults | Notification.DEFAULT_VIBRATE;
// defaults = defaults | Notification.DEFAULT_SOUND;
mBuilder.setDefaults(defaults);
mBuilder.setSound(Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.siren));
// Cancel the notification after its selection
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
I also want notifications with sound and vibration when my app is in background.
Generally speaking you can't. Even notification with MAX priority wont be shown in DND mode.
The possible workaround that I can imagine is to use android.permission.SYSTEM_ALERT_WINDOW to draw custom notification over system window (FB Messanger works in similar way) : Creating a system overlay window (always on top) .
But this method is suitable only in rare cases and most of the time it's a violation of Android UI/UX best practices.
Ok good news you are half way there.
I achieved this previously by using AudioManager & VIBRATOR_SERVICE (yea I know ;)
The idea is not to use NotificationCompat.Builder because it will rely on system settings and if device is in silent mode, it won't vibrate nor play sound. You have to manually play a sound using AudioManager & vibrate using VIBRATOR_SERVICE.
You are using AudioManager & setting the properties but you are never actually asking it to play the sound. So, in your code its never actually utilized.
Here's an example of how to play a sound using AudioManager, it will also ignore silent mode:
Here's an example of using VIBRATOR_SERVICE:
Combine these 2 approaches and ditch NotificationCompat.Builder
If you can get right permissions to change settings of device, then you could show notifications with sound and vibration at your apps. Also check this out.
This may be off topic , but I couldn't found anything for it.
Is there any limit on the number of notifications android app can display?I am facing issue after 100 notifications. There is no documentation which states this clearly.
Note: This is not really a good idea to show 100 notifications but It is required for certain reasons.
According to #Nirel's answer.
1) I tried to run the code in 3 different devices.
Surprisingly notifications beyond 50 are not showing in notification area.
It gives following error.
W/NotificationManager﹕ notify: id corrupted: sent 51, got back 0
The same error comes for subsequent calls.
I saw the source of NotificationManager , it gives this error if incoming and out id is not same. See below code.
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/NotificationManager.java#L233
2) After I tried to notify on intervals of 100 milliseconds.
It also Gives the same error. What I tried is removed 1 notification when code is executed.
Surprisingly , notification number 153 came in status bar.
So the conclusion is that , at most 50 notifications can be there. This may be default behaviour and may can change by manufacturer as said by #Sharp Edge.
Thnx.
In API23
package com.android.server.notification;
NotificationManagerService.java
static final int MAX_PACKAGE_NOTIFICATIONS = 50;
The limit for notifications and toasts is per app 50
this post has really helped me to do research on this topic. I have written an article on this like how can you modify your logic and keep posting notifications even if you have reached the maximum limit by compromising on the oldest notifications. https://medium.com/mindorks/the-notification-limit-per-app-in-android-94af69a6862c
The notification limit dropped from 50 to 24 per appin the Android 10 notification drawer.
Read more about it here.
run this:
// prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiver.class);
// use System.currentTimeMillis() to have a unique ID for the pending intent
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
for(int i = 0;i<1000;i++)
{
Log.d("Tag", "notification number" + i "just published")
notificationManager.notify(i, n);
}
when the application will crash you will see how much notification you have..
So I am writing an app that will use the LED to notify users of anything when the screen is OFF. Below is how I used it to work:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.ledARGB = 0xFFFFFF00;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
notification.ledOnMS = 100;
notification.ledOffMS = 100;
notificationManager.notify(Constants.LED_NOTIFICATION_ID, notification);
It works fine, but problem occurs when my device (Galaxy S5) has missed calls, and the stock LED notification for missed calls overrides my LED setting all the time. Is there a way to override the stock LED settings, and have it display mine instead? I've tried fiddling with
notification.priority
but that doesn't seem to do anything. Any help would be appreciated. Thanks!
Is there a way to override the stock LED settings, and have it display mine instead?
Not in general. Control over the LED is up to the device manufacturer, not you. Or, quoting the documentation, "Since hardware varies, you are not guaranteed that any of the values you pass are honored exactly."
Also, bear in mind that not all devices have LEDs that a Notification can control, sometimes because the device does not have an LED at all.
I'm writing a program that monitors several different events and creates status bar notifications when certain conditions are met. The idea is that based on the specific condition there are different notification sounds played, so that the user would know what is happening without having to look at the phone. All notifications are created with different id's (right now random numbers are used, though I may change that later) so they do not override one another. All show up in a status bar and play their notification sounds appropriately.
So far so good, but here is a problem. The program main loop in a service sleeps for set amount of time, then wakes up and checks all conditions, creating necessary notifications. It often boils out to having to create two or more notifications in a row. Here comes the problem, only the sound alert of the last notification that is popped up is being played. So the user will never know there was more than one notification about different events just from the sound.
Any way this can be remedied? Thanks in advance!
UPDATE:
After reading some comments and messing around a bit more, it looks like I know WHY this is happening, but not HOW TO FIX it. So the question is still open as of this update. When two notifications popped up as the phone pretty busy (read slow to respond), I could hear the beginning of first sound that was then taken over by the second one. So it looks like the system is honestly trying to play the first sound, but when second notification comes up it simply puts the new sound up cancelling the first playback... So I guess I need to try and find out if there's a way to tell the system to play them in sequence, not all at once...
UPDATE2:
For now - added code to make sure there is at least 5 seconds between any two notifications being created, and it does solve the problem. As I mentioned in comments below, I don't think it's the best solution, so while this allows me to continue working on the project, I'm still looking for a better way to do it.
Here's the code that creates a notification:
public void ShowNotification(String Ticker, String Title, String Text,Hero H, Uri Sound, int Icon){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
CharSequence tickerText = Ticker;
long when = System.currentTimeMillis();
Notification notification = new Notification(Icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound=Sound;
Context context = getApplicationContext();
CharSequence contentTitle = Title;
CharSequence contentText = Text;
Intent notificationIntent = new Intent(this, AlertActivity.class);
notificationIntent.putExtra(getString(R.string.FROM_NOTIFICATION), true);
PendingIntent contentIntent = PendingIntent.getActivity(this, rnd.nextInt(), notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
try{
mNotificationManager.notify(rnd.nextInt(), notification);
} catch(Exception e) {
Log.w("Service", "Notification Exception: " + e.toString());
}
}
if(yourCondition)
notification.sound = android.net.Uri.parse(sharedPrefs.getString(CommonDefn.PREFERENCE_NOTIFICATION_RINGTONG, Settings.System.DEFAULT_NOTIFICATION_URI.toString()));