I'm developing application, in that I'm using push notification. For that I want to add setting to enable/disable notification sound. I am used ToggleButton for Yen/No.
Now on Yes, I declared,
`defaults = defaults | Notification.DEFAULT_LIGHTS;
defaults = defaults | Notification.DEFAULT_VIBRATE;
defaults = defaults | Notification.DEFAULT_SOUND;`
But when it NO, i dono how to Mute it.
Please give me suggestions to mute sound.
Add this before:
myNotification.defaults = 0;
myNotification.defaults |= Notification.DEFAULT_VIBRATE;
Notification.DEFAULT_LIGHTS
Notification.DEFAULT_VIBRATE
Notification.DEFAULT_SOUND
Notification.DEFAULT_ALL
Related
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.
I developed android app which sends a notification in a particular time. The notifications works great and it shows arabic and english text well. but when i run the app on android device Nexus 5 with android version 5.1.1, the text notification in arabic appears as in a photo
Here's the code i used..
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher,ticker, System.currentTimeMillis());
notification.setLatestEventInfo(activity, title, text, pendingIntent);
// Cancel the notification after its selected
notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
// notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
notification.flags |= android.app.Notification.FLAG_AUTO_CANCEL;
notification.defaults |= android.app.Notification.DEFAULT_SOUND;
notification.defaults |= android.app.Notification.DEFAULT_VIBRATE;
NotificationManager notificationManager = (NotificationManager)activity.getSystemService(activity.NOTIFICATION_SERVICE);
notificationManager.notify(Id++, notification);
Try To use Arabic Reshaper
I used it to display arabic in law APIs before
https://github.com/agawish/Better-Arabic-Reshaper/blob/master/src/org/amr/arabic/ArabicReshaper.java
The notification bar in my application shows only the small icon in the ticker (as it should). However, when the "shade" is pulled down, it shows both the small icon from the ticker, as well as a large icon that I set in the Notification.Builder. Here's my code:
if (Build.VERSION.SDK_INT > 10){
notification = new Notification(R.drawable.ic_stat_mintchip,
"This is a test",
System.currentTimeMillis());
notification.largeIcon = (((BitmapDrawable)c.getResources().getDrawable(R.drawable.ic_launcher)).getBitmap());
notification.defaults |= Notification.DEFAULT_ALL;
notification.number += 1;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
} else {
notification = new Notification(R.drawable.ic_stat_mintchip,
"This is a test",
System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_ALL;
notification.number += 1;
}
}
I don't quite know why this is happening. Any assistance?
I think the issue here is possibly that you're not using the Notificaiton.Builder class. Here's a small example of what you could do (you would have to insert your own variables though, and set the other properties that you used such as vibration):
Notification.Builder nb = new Notification.Builder(context)
.setContentTitle("title")
.setContentText("content")
.setAutoCancel(true)
.setLargeIcon(largeIcon)
.setSmallIcon(R.drawable.small_icon)
.setTicker(s.getText());
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(100, nb.build());
Another issue i had in android lollipop is that the small icon was displayed next to the large icon. To solve it - just don't set the large icon! Use only the small icon setting.
http://developer.android.com/guide/topics/manifest/uses-sdk-element.html
if you set your max sdk version in android manifest to 19(i.e KitKat) Your app will no longer display notifications meant for lolipop
I'm using a notification to let the user now that the service is still running. Now I'd like to use the notificationlight to remind the user. (because it's fancy)
The notification works fine, but the notification light does nothing. Other applications work fine with the notification light, (gtalk, facebook)
it's more or less the example code for notifications with addition of these flags:
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 100;
notification.ledOffMS = 100;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.flags |= Notification.FLAG_NO_CLEAR + Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(NOTIFICATION_ID, notification);
and
notification.defaults |= Notification.DEFAULT_LIGHTS;
instead doesn't work either.
I'm debugging on a Galaxy Nexus with Android 4.0, but the app's target is Android 2.3.3
EDIT:
could this be a problem of permission? If yes, which one? I looked through all and found no matching permission for the notification light.
I think there is an error with the + operator, you need the OR:
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
EDIT: and if you are using flags, I think the right one should be:
notification.flags |= Notification.FLAG_SHOW_LIGHTS
On jelly bean devices, led only works if notification priority is set to max or default, please check again. Following snippet of code is working fine for me on jb devices.
notification.setLights(0xFF0000FF,100,3000);
notification.setPriority(Notification.PRIORITY_DEFAULT);
Here I'm showing blue color led for notification which will remain on for 100 ms and off for 3000 ms till user unlocks his device.
And check if you are using NotificationCompat (compatibility) class than ignore setDefaults method and use SetLight, SetSound, Setvibration, etc
I am using Galaxy Nexus with Android 4.0, I set the silent mode to vibrate in Settings. I use NotificationManager.notify to send the Notification. I don't set Notification.vibrate, I even use myNotification.defaults &= ~Notification.DEFAULT_VIBRATE to disable the vibration. But it still vibrate after calling NotifcationManager.notify.
Could anyone tell me how to turn off the vibration of a Notification in vibrate mode?
use the following code:
notification.defaults = Notification.DEFAULT_LIGHTS;
//or
notification.defaults = Notification.DEFAULT_SOUND;
To disable Vibration when notification comes, I use this code.
notification.vibrate = new long[] { -1 };
And its working perfectly.
To manage notification settings dynamically:
notification.defaults = Notification.DEFAULT_LIGHTS;
if(/*sound enabled*/)
notification.defaults |= Notification.DEFAULT_SOUND;
if(/*vibration enabled*/)
notification.defaults |= Notification.DEFAULT_VIBRATE;
First store the value of your vibrate setting button in shared preference. and then place this code where your notification is received.
SharedPreferences preferences = context.getSharedPreferences("VIBRATE",
0);
boolean vibrate = preferences.getBoolean("vibrate", true);
if (vibrate) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
It can also be done using the following adb command for specific package
adb shell
cmd appops set package_name VIBRATE ignore
The above command will disable all vibrations for package_name package.