Enable notification channels programmatically - java

I am able to check if the channel is enabled/disabled with
NotificationChannel channel = manager.getNotificationChannel(channelId);
boolean isEnabled = channel.getImportance() != IMPORTANCE_NONE;
But if the channel is not enabled then I would like to enable it by setting it to IMPORTANCE_HIGH
if (!isEnabled) {
channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
The problem is that the channel is not updated, it only works if I tried to disable it with IMPORTANCE_NONE in case it is enabled, but not the way around.
I have tried to delete the channel and create a new one with a different id, that works, but not creating a new one with the same id.
How to get around this?

You can't change channel importance programmatically without deleting the channel.
Because user may have changed the importance manually.
To achieve this programmatically to get channel and create new channel with new id.
delete the old channel.
Your changes will not reflect if create channel with previous id
for reference check WhatsApp application try to change the ringtone from application and see in channel at bottom left x channel is deleted message

Related

set custom Sound from only my application in push notification android

I need to set custom sound for only my application. I've assigned a custom ringtone from my storage to be used in notifications.
What did I try...
(NotificationChannel) channel.setSound(Uri.parse("MY_PATH"), audio_atrr);
(NotificationCompact.Builder) mBuilder.setSound(Uri.parse("MY_PATH"));
(Notification) mNotification.sound = Uri.parse("MY_PATH");
It's useless for me, my device always plays the default sound when a notification is coming. what can i do?

Discord4j move user to another channel

I have users connected to the voice channel "waited" and I would like to move them to another channel. How to do it? Is it supported by discord4j?
Look at GuildMemberEditSpec. There is a command setNewVoiceChannel that can be used to move the user to a new voice channel.

How to get the sound used for a notification inside NotificationListenerService?

I am using a NotificationListenerService to receive notifications. I use the method onNotificationPosted(StatusBarNotification sbn) to see when a new notification is, well, posted.
This all works great, however, I cannot get the sound uri from the notification.
Here is what I have:
int notiDefaults = sbn.getNoficiation().defaults //This is always 0
Uri sound = sbn.getNotification().sound; // This is always null
However, when I create my own test notification and explicitly do the following:
ncomp.setDefaults(Notification.DEFAULT_SOUND); // This makes the above code yield 1 for notiDefaults, does not change sound uri but is fine I can get the default uri easily
ncomp.setSound(myUri); // This makes the above code yield the value of myUri for "sound" when notificationPosted is called
The main problem I am having is this:
int notiDefaults = sbn.getNoficiation().defaults //This is always 0
Uri sound = sbn.getNotification().sound; // This is always null
occurs despite the notification actually playing sound (ie, Textra) plays sound by my NotificationListenerService says that it does not.
However, even in my test notification, if I do not explicitly set the sound or defaults, (meaning the notification channel says its type alert and will play a sound) the NotificationListenerService always yields null for sound uri.
What I am discovering is that no matter what I do in my onNotificationPosted method, if the notification does not explicitly set the defaults or sound uri, I cannot retrieve them. If one uses the notification channel options under android settings to set the sound, any NotificationListenerService will not get the notification sound uri to use for that notification.
If I had access to the NotificationChannel that belongs to this Notification then I can determine everything needed. But, there is no method which allows third parties to get a NotificationChannel of another app.
This seems to be broken google code or I am missing something.
EDIT: Testing on Android 11 Samsung Galaxy S20+
Thanks all!

Foreground Service with a Silent Notification

Context:
I have been using Google's LocationUpdatesForegroundService example project to learn a bit more about services.
I have downloaded the project via Github desktop and ran it on my phone, everything is great and the project does what it's intended to do.
My phone is Android 8.0.0, API 26
Problem:
The foreground service notification shows up on the status bar once the app is terminated, as soon as that happens I hear a notification sound(default sound).
However, I would like the notification to be silent, like in some location-based apps(eg: Life360)
What I've tried so far:
in LocationUpdatesService.java at added159 tried mChannel.setSound(null,null);
in LocationUpdatesService.java at line 296 changed .setPriority(Notification.PRIORITY_HIGH) to
.setPriority(Notification.PRIORITY_LOW)
in LocationUpdatesService.java at line 158changed NotificationManager.IMPORTANCE_DEFAULT to NotificationManager.IMPORTANCE_LOW)
in LocationUpdatesService.java at line 300 added setSound(null)
None of the above have worked for me, I would really appreciate if someone could shed some light on this situation.
The solution is to use NotificationManager.IMPORTANCE_LOW and create a new channel for it. Once a channel is created, you can't change the importance (well, you can, but the new importance is ignored). The channel information appears to get stored permanently by the system and any channel created is only deleted when you uninstall the app. you can delete the channel as
nm.deleteNotificationChannel(nChannel.getId());
and recreate it with
nm.createNotificationChannel(nChannel);
via stack overflow answer here Disable sound from NotificationChannel
In the LocationUpdatesForegroundService, when the service is onUnbind, it will invoke 'startForeground(NOTIFICATION_ID, getNotification())', then the Notification will show with sound. So you need to change the NotificationChannel follow the below code
mChannel.setSound(null, null);
mChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
mChannel.setSound(null,null) will do it, but you need to uninstall/reinstall the app for it to take effect. Or changing CHANNEL_ID to a different value will also recreate channel with updated setting.
I might be a little late with answer, but did you try reinstalling the app? It's simple and it may fix problem.
Maybe I am late to answer this question
but for other seaker for an answer, i will add my answer:
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this, CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setSilent(true);

GCM - How to send notification to multiple device targets with different message contents in java

I need to send different message contents to different device targets
final List<String> androidTargets = new ArrayList<String>();
serverSender.send(gcmMessage, androidTargets, 1);
I can add multiple device targets with same message however I need to add multiple messages related to that target like:
List<String> messages
Message index need to be mapped to device target
Is there any possibility to achieve this? I need this since I want to send notification in a single shot instead of queuing in my side!

Categories

Resources