How to properly update notification channel android oreo - android

I'm trying to update a notification channel after I create it. Namely, I want to properly set the notification sound, but after I create it. I can't really figure out the proper way to do this.
What I tried was to delete the channel and re-create it but it doesn't seem to be working...
if (notificationManager != null) {
notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID);
notificationManager.createNotificationChannel(channel);
System.out.println("Created notification channel" + channel.getSound() + " " + channel.getImportance());
}

It is correct like stated in the other answer that you can only alter the name and description of the channel once it has been created. However, as you point out in your code you can delete the channel and then create it again. But if you create the same channel once again but change something, for example the sound, it wont work. I'm assuming Android is preventing that change the same way it does if you try to create it when it already exists. So Android must have a way of keeping track of all the deleted channels (in other words, they are not completely deleted).
If you look at WhatsApp they allow you do change the sounds from within the app and if you investigate a little you'll find that they are indeed deleting and creating channels.
So what can you do? What you can do is that you can alter the ID of the new notification channel. Perhaps adding a big enough random element would prevent you from ever having the same ID twice. Or increment something and store that information within your app (prefs or db or something). If the "recreated" channel has a new ID Android will accept your "changes". Since you're not altering an existing channel, you're creating a completely new one. And if you keep the rest of the user-visible information intact (such as name, description etc) then the user will not notice this but only experience that the sound of that type of notification (channel) was altered from within the app.
Any downsides? Well, a superminor one is that Android will show in the App notification settings how many times a channel has been deleted (to alert the user of "spamming"). I don't think many users care about that. And you're also deviating from the design that Android wants (full user control of the channels) which perhaps might not be desirable.
So from how you describe your usecase I think it's fair to do it this way to accomplish what you want :)

Once a notification channel is created, the user has ultimate control over its settings. As the developer, you can only change the channel's title and description. If you want to recreate the channel, you have to use a different id.
See: https://developer.android.com/training/notify-user/channels

Related

Can an app read information from the lock screen?

I should start by saying that I'm not (yet) an Android developer. I've got a very specific, personal task that I'd like to achieve. If what I want to do is possible then it will be worth me learning enough to make it work. If it's impossible then I'll stick to what I already know!
I have type 1 diabetes and use a medical app which displays my blood glucose level on an Android phone's lock screen. There's a screenshot below. I'd like to be able to capture the information (particularly the number in the red box) and use it in other ways.
I've tested another app that reads Android notifications, but it only seems to have access to the popup messages that appear from an app. It doesn't see the persistent notifications on the lock screen.
So... is it possible for an app to read those lock screen notifications?
Thanks in advance for your help.
Yes that is possible with NotificationListener:
https://developer.android.com/about/versions/android-4.3.html#NotificationListener
The flags FLAG_ONGOING_EVENT or FLAG_NO_CLEAR will let you know if a certain notification is persistent.
Here is a sample project that I found useful demonstrating NotificationListener. In the NotificationListenerExampleService.java, you can simply change the onNotificationPosted method to find out if a notification is persistent:
#Override
public void onNotificationPosted(StatusBarNotification sbn){
if (sbn.getPackageName().equals("package name of your desired app")) {
if (!sbn.isClearable()) {
Notification notif = sbn.getNotification();
// get your desired data in the notif object
}
}
}
Note that you can see the package name of an app by just simply going into its Google Play store page, and in the url you will find something like this:
https://play.google.com/store/apps/details?id=chat.rocket.ne2.reactnative&hl=en_CA
where the id field (chat.rocket.ne2.reactnative) is the package name.

Huawei EMUI 8 Oreo - Notification Channel - Tone

Some users are reporting on Huawei EMUI 8.0 Oreo that the Notification Channel does not have the option to change tone "Sound" / "Notification Tone" from the app!
As of the official docs the developer can't change the settings anymore.
So how can I add an option for Huawei phones to change the "Notification Tone" again?
And does anyone know why the hell Huawei removed this feature?
I don't find official docs from Huawei how we can now let the user change the notification tone.
Offical docs I am referring:
https://developer.android.com/reference/android/app/NotificationChannel.html
https://developer.android.com/reference/android/app/NotificationChannel.html#setSound(android.net.Uri, android.media.AudioAttributes)
We ran into the same issue recently.
It is not a nice solution, but WhatsApp is doing the same.
Basically we show a ringtone selection inside our app and then delete and recreate the notification channel with a new channel id and the selected ringtone uri. You can copy most settings made to the channel to the new one except 'do not disturb'.
Like I said it is not a nice solution and I don't know what will happen if the channel is recreated a lot. But hopefully the ringtone is not changed too often.
Note: The notification settings screen displays the number of deleted channels, as a spam prevention mechanism.
Faced with same issue on chinese devices. Firstly, i have same solution like describe #Devenias. How it works in system: when you're defining a new channel, NotificationService save this channel in xml, after you changed it, it still contains in this xml. So on a new change of channel, NotificationService will check if it have a channel with a same name, and just retrieve it. So i make new channel with a new settings all the time, when user change vibration or ringtone in app. Also it works like a cache, just make unique channel name for pair<ringtone, vibration>. This solution is pretty hacky, since it works good on Honor's, Huawei's, Samsung's devices and Xiaomi Mi A1, but it have been crashing NotificationService with NullPointer in SystemUI on Xiaomi Mi Mix 2 (device make soft reboot, if SystemUI service crashed), so don't use this solution.
So nowadays a safe workaroud is to play sound and vibration manually.

Cast Receiver App does not show subtitles

According to the Release Notes (of July 8), the docs for the Sender and the updated answer of this question, the Styled Media Receiver of Google Cast does now support Closed Captioning or Subtitle tracks.
However, when I tell the Default or the Styled Media Receiver to show a text track, nothing happens. It does not even load the .vtt from the server, as I can see in the logs.
I can tell the receiver app got the text tracks just fine, but even using the Android example app, the subtitles never show up. According to all the logs, they are being sent and the receiver app is told to show them - but they never appear, they are never even loaded.
The MediaTrack is being created as follows:
new MediaTrack.Builder(2, MediaTrack.TYPE_TEXT)
.setName("Deutsch")
.setSubtype(MediaTrack.SUBTYPE_CAPTIONS)
.setContentId("https://example.com/video/caption_de.vtt")
.setContentType("text/vtt")
.setLanguage("de").build();
I have checked thrice that the file exists and is being loaded with the type text/vtt. But that does not matter, as the file is never even requested by the player. I have tried both MediaTrack.SUBTYPE_CAPTIONS and MediaTrack.SUBTYPE_SUBTITLES.
So I need to know, is this claimed support of CC in the Styled Media Receiver simply a lie? Or is there some undocumented trick required to make it possible?
If there is still a custom receiver required, I would like to know how to convert the example player to support subtitles, as it doesn't seem to support them either.
First, I suggest you change your wording in future posts (re: "..is simply a lie.."); that is not appropriate at all. Secondly, it works and you can test that with the CastVideos-android app (or ios variation of it for that matter); the first three videos have CC. Lastly, we have documentation on that subject on our documentation site (https://developers.google.com/cast/docs/android_sender, under "Using the Tracks API").

How to add an element to the pull down notification bar of an existing app?

I want to add a mark as read option to the pull down notification bar of Gmail but I don't have the slightest clue about where to start. If somebody can tell me how to do it or point me in the direction of the right docs to pull it off it would be much appreciated.
I know it is possible because it has already been done, but I want to do it by myself.
That's quite an undertaking.
So the link you provided has a bit of a walkthrough in its description about where to start. The trick is that because you're trying to program a notification service for an EXISTING app, you don't really have control over the notifications the app itself creates. I suspect what you're going to have to do is program a NotificationListenerService, listen for gmail notifications, and somehow cancel the gmail notification and replace it with one of your own, as created through the android documentation.
For a good example of how NotificationListenerService works please take a look at this:
https://github.com/kpbird/NotificationListenerService-Example
The canceling is something I have not tested but you asked for ideas, not code. NotificationListenerService has a method cancelNotification(String pkg, String tag, int id) which looks like you can use to cancel the gmail notification.
You can get access to the raw Notification-object using an AccessibilityService. I haven't tried it before to be honest, but I assume you can modify the Notification and add your own buttons, etc.
More information on tinkering with the Notification-object in an AccessibilityService here: https://stackoverflow.com/a/10303676/198996

Access the android notifications of other applications

Is it possible to programatically access the text posted on the android notification area (displayed by an application which is not my own)?
The logic I am after would be something like...
for each (NotificationMessage m in NotificationArea.Notifications)
{
String msg = m.GetMessage()
}
Unfortunately, no. This would create security concerns, possibly hijacking personal information. See This Thread
In Android 4.3 and above, use the NotificationListenerService. Otherwise use Accessibility.
http://developer.android.com/reference/android/service/notification/NotificationListenerService.html
This is possible with the accessibility service, but you have to pick up on notifications as they arrive (ie there isn't a list you can iterate around at any point). You'd need to build that up yourself.

Categories

Resources