How to make Notification Silent using RingtoneManager - java

I have added a checkbox preference in preferences.xml. There is a settings.java file which has defined all the preferences.
Now There is a seperate java file which displays a notification when a message comes in.
If the checkbox is true, I would like to make the notification silent.
if(PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getString(Settings.PREF_SILENT_MODE, "").equals("true"))
//Make the notification silent ie.
else
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound = alarmSound;
There is a TYPE_NOTIFICATION method associated with Ringtone manager for notification type of sound. Is there a way to make this silent?
I know there is a way to make it silent using AudioManager. setRingerMode(AudioManager.RINGER_MODE_SILENT);
But here, ringtone manager is being used.
Edit--FIGURED IT OUT
(flag is a boolean value which returned the checkbox preference result from SharedPreferences)
settings.java file-
public boolean checksilentmode(Context mContext)
{
SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(mContext);
return prefs.getBoolean(PREF_SILENT_MODE,DEFAULT_SILENT_MODE);
}
Another java file-
`if(flag) //if flag is true ie silent mode is enabled.
{
alarmSound=null;
}
else
{
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
notification.sound = alarmSound;`

No need to do all of this ...if you are showing notification using NotificationCompact.Builder do something like below
notificatiomCompactBuilder.setDefaults(4);

Related

Android 13 NotificationCompat setSound STREAM_ALARM not using Alarm volume

I am creating an app which when needed would send a notification with sound. It is important that the notification would be heard even on silent or "do not disturb" so I want to ride the phone's Alarm volume.
I'm using and modifying code from an open source package react-native-alarm-notification
Here's some of my code-
// some code for creating the alarm object and getting the settings
// creating the builder
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelID)
.setSmallIcon(smallIconResId)
.setContentTitle(title)
.setContentText(message)
.setTicker(alarm.getTicker())
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(alarm.isAutoCancel())
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setSound(null)
.setDeleteIntent(createOnDismissedIntent(context, alarm.getId()));
if (alarm.isPlaySound()) {
// THE IMPORTANT BIT
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE),
AudioManager.STREAM_ALARM);
}
...//more settings
// set tag and push notification
Notification notification = mBuilder.build();
String tag = alarm.getTag();
if (tag != null && !tag.equals("")) {
mNotificationManager.notify(tag, notificationID, notification);
} else {
Log.i(Constants.TAG, "Notification done");
mNotificationManager.notify(notificationID, notification);
}
The problem is that the notification still defaults to Android 13's notification sound setting, and not using the Alarm volume setting.
Well I don't know if what I tried to do is possible but I found a different solution.
Basically I created a separate audio interface using MediaPlayer, that whenever a notification is sent, it also starts an alarm, and when the notification is dismissed, it stops the alarm.

How to open android default apps activity in setting via Intent?

How to open the default handler page in the android setting via intent?
or How we can open directly the default SMS page?
it seems from android Q to upper we can not Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT
Found the answer, you can open this page by this code:
//This code will work only on android version N and above
//Add if condition to prevent crash on older devices
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent intent = new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (requestCode != -1)
act.startActivityForResult(intent, requestCode);
else
act.startActivity(intent);
}
Another way with this code to open an intent default app,
in this case, I try to set the default launcher app
// dialog set default app
#RequiresApi(Build.VERSION_CODES.Q)
private fun showLauncherSelection() {
val roleManager = this.getSystemService(Context.ROLE_SERVICE)
as RoleManager
if (roleManager.isRoleAvailable(RoleManager.ROLE_HOME) &&
!roleManager.isRoleHeld(RoleManager.ROLE_HOME)
) {
val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_HOME)
startActivityForResult(intent,0)
}
}
You can set the other Default app with change the ROLE_HOME following this Role Manager. If you want to change SMS default app, try to change the ROLE_HOME to ROLE_SMS

Is it possible to use createChooser but not actually starting the intent (yet)?

In my app I have a share button which allows the user to share some text through other apps such as Messenger. I use createChooser to let the user select the app they want to use. The tricky part is, after they've selected the app, I don't want to start the selected app immediately, but I need to do some communication with the server first (to get the exact text to share), and I also want to prompt some message on the screen at this stage, and then I can start the share intent of the selected app.
In other words, the chooser is simply an interface for selection, I don't want it to actually start the intent; I will start it manually a little later.
I tried to modify the intents that are collected into the chooser in such a way that they simply redirect back to my own app. However, I have problem retaining their icons and label. No matter what I do, some of the selections always change their icons and labels to those of my own app.
Here's roughly what my current code looks like:
PackageManager pm = activity.getPackageManager();
Intent sendIntent = new Intent()
.setAction(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, text)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setType("text/plain");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, PackageManager.MATCH_DEFAULT_ONLY);
List<LabeledIntent> intentList = new ArrayList<>();
for (ResolveInfo ri : resInfo) {
String packageName = ri.activityInfo.packageName;
String name = ri.activityInfo.name;
if (name.contains("facebook.messenger")
|| name.contains("naver.line")
|| name.contains("android.mms")
|| name.contains("clipboard")
) {
ComponentName cn = new ComponentName(packageName, name);
Intent intent = new Intent(sendIntent)
.setPackage(packageName)
.setComponent(cn); // I tried to do somthing here but it doesn't work
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
// loadLabel and icon doesn't always get the correct thing
}
}
Intent first = intentList.remove(0);
Intent[] extra = intentList.toArray(new Intent[0]);
Intent receiver = new Intent(activity, ShareReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(first, title, pendingIntent.getIntentSender());
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extra);
activity.startActivity(chooser);
"but I need to do some communication with the server first"
Do the communication earlier, store and use it when required?
"the chooser is simply an interface for selection, , I don't want it
to actually start the intent; I will start it manually a little
later."
Have you heard of ModalBottomSheet reference to implement the custom chooser for yourself as required.
"I tried to modify the intents that are collected into the chooser in
such a way that they simply redirect back to my own app."
Use startActivityForIntent in that case; when you intent an app, close and return back to your app, handle it onActivityResult whatever you want to do.
" However, I have problem retaining their icons and label."
Are you referring to the icons and labels of other apps in the chooser dialog?

List all the available Notifications Ringtones in Android

I'm trying to change the default sound of the Notification and I do like this:
private void showNotification(Context context, String reminderid, String title, String shortinfo, String longinfo)
{
mNM = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NOTIFICATION=Integer.parseInt(reminderid);
Notification notification = new Notification(R.drawable.icon, title,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, RemindersActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
notification.setLatestEventInfo(context, shortinfo,
longinfo, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
notification.defaults |= Notification.DEFAULT_VIBRATE;
mNM.notify(NOTIFICATION, notification);
}
But you can notice that I'm giving number the ID "6" for the URI.withAppendedPath(), I need to list all the available Notifications Ringtones for the user and let him choose, and I will pass the ID of what he choose instead of "6".
Here Google says:
In this case, the exact ID of the media file ("6") is known and appended to the content Uri. If you don't know the exact ID, you must query all the media available in the MediaStore with a ContentResolver. See the Content Providers documentation for more information on using a ContentResolver.
How can I do what they say (note that I never worked with content providers or resolvers)? and give the user the option to choose the ringtone for notification like choosing it in phone settings?
Thanks in advance.
You can add sound to your raw folder, initialize it
MediaPlayer mpSplash = MediaPlayer.create(this, R.raw.slow);
and call it where ever required
mpSplash.start();

Android: Managing Multiple Notifications

I am trying to create multiple notifications in my application. To identify each notification uniquely, i have given them an unique identificationId. Following is my code:
private void updateNotification(int notificationId, int clockStatusID, CharSequence text) {
//notificationManager.cancel(notificationId);
// throws up an ongoing notification that the timer is running
Log.i("TIMERCOUNT", "Notification id: " + notificationId);
Notification not = new Notification(clockStatusID, // the
// icon
// for
// the
// status
// bar
text, // the text to display in the ticker
System.currentTimeMillis() // the timestamp for the
// notification to appear
);
Intent intent = new Intent();
intent.putExtra("notificationID", notificationId);
intent.setAction("actionstring" + System.currentTimeMillis());
intent.setClassName("com.chander.time.android.activities",
"com.chander.time.android.activities.Tabs");
not.setLatestEventInfo(self,
getText(R.string.timer_notification_title),
getText(R.string.timer_on_notification_text), PendingIntent
.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT));
not.flags += Notification.FLAG_ONGOING_EVENT;
not.flags += Notification.FLAG_NO_CLEAR;
notificationManager.notify(notificationId, not);
}
Problem:
When a notification is selected, Tabs activity is called passing the intent. I want to access the unique notificationId of the notification that was selected in Tabs. I tried intent.putExtra() to save the notificationId in the intent. But, for multiple notifications its overwriting the notificationId and returns the latest one. I dont understand as to why this is happening and how can i avoid this overwriting of notificationId.
Thanks,
Chander
I got the answer. The intents were getting cached in. To, make a new intent, just add the following piece of code:
saveCallIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
This makes the intent unique.
Also, someone suggested me the following piece of code to make the intent unique:
saveCallIntent.setAction("actionstring" + System.currentTimeMillis());
It didnt help me, but might be of help to someone else.
--Chander
just place the notificationId instead of 0 in pendingIntent.
PendingIntent.getActivity(this, notificationId, intent,PendingIntent.FLAG_UPDATE_CURRENT));
The user must also update the following code together with the above one to get it working.
mNotificationManager.notify(notificationId, mBuilder.build());

Categories