I have an Ongoing notification that shows Date and updates every minute inside a service. But in android 8 it plays sound in every update and users confused whats happening.
i set this:
builder.setPriority(NotificationCompat.PRIORITY_LOW);
This disabled the sound but has a side effect. Updating the Date is not working too! I want to update Notification without any sound. how it is possible?
In your notification channel try to set the sound to null. Like so:
channel.setSound(null, null);
Related
I want to create a custom notification like inshorts as shown in Image below
And i am able to achieve that as well, shown in screenshot below :
But there are two problem cases:
When i install the app using Android Studio, initially it works fine i.e. Custom Notification layout is displayed for the notification(as shown in picture-3), but when i am killing the app from task manager and then i am sending the notification, it comes as default layout(as shown in picture-4).
When i install the app using the apk on any device, i am getting the notification style as default notification(as shown in picture-4).
I have been stuck in this for long and have changed the whole code of Notification, i am still facing the issue. I am not sure where i am doing wrong.
Picture-3
Picture-4
I was able to achieve what i was looking for i.e. I was looking forward to use the custom layout for showing Notifications eveytime, but the problem was that i was getting custom layout notification if the app used to be in the background and the default notification style when the app is not in the background.
"So what exactly the problem was, Push notifications behave differently in the cases where the app is in background or foreground."
When the app used to be in background the Notification builder code which i have written was being used and the notification with custom layout was displayed. Moreover, when the app used to be in foreground, the android system couldn't reach the notification builder code of my project, in that case notifications were being processed by the Google Service process, which uses the default notification layout.
Solution :
Earlier, the JSON object which i was getting from the server was named as "notification", which generally is used by almost every developer. Changing the name of that JSON object to "data" did the magic.
And the code inside the Notification builder used to be something like this:
String notTitle = remoteMessage.getData().get("title");
which ealier used to be:
String notTitle = remoteMessage.getNotification().getTitle();
This way, my notifications are always being handled by the app, by the NotificationService, and not by the Google Service process.
I hope this helps somebody. :)
I want to program a music controller in my app, so I would be able to play/pause, skip to the next song or to the previous song in my playlist from another built-in music app. I've seen a solution using broadcast to send messages to all music players to, for example, start playing music. The problem is, music starts playing on three different music players I have installed. Does anybody know what to use instead of sendBroadcast, so that message will be sent only to the app I want?
I found the following solution on the internet. It uses the sendBroadcast method. The message is broadcasted to every music player which I do not want. I want it to be sent only to one specific app.
long eventTime = SystemClock.uptimeMillis();
/*NEXT*/
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventTime, eventTime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendBroadcast(downIntent, null);
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventTime, eventTime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendBroadcast(upIntent, null);
You cannot send it to just one music player. A broadcast is a system event that triggers an action that other apps can subscribe to. Im pretty sure its very possible to write a music player that does not subscribe to those broadcasts unless Android forces that on you when playing back music (Im not sure wether or not it does, but I think not). So, you are not even sending it to everybody, you send it to the system and whoever subscribed to it will then be able to act on it.
What exactly is the usecase of this app?
Another way you can interact with other apps is an implicit Intent. With that you can call views from other apps and get a result back. This only works with apps/views where the developer of that app allowed this. And does not do what you want either.
I already asked about it before, but my question even after edits about my progress of fix this noone answered to it anymore. This is original question:
Android media player stop playing while in background
So in short, I'm making music player app, but mediaPlayer stop sometimes when loading next song. After many tests I find out that it stops at mediaPlayer.prepare() and it won't continue as long as I don't trigger any action on phone like turning on display, volume up/down, click headset button. I'm out of ideas what can be a reason of it.
maybe you need to call prepareasync() instead of prepare().
I've figured out that the MediaPlayer on Android 4.4.2 seems to fade-in the Audiofile automatically.
I am using the MediaPlayer for playing a Sound that mustn't be faded in.
It worked well with a Smarthpone with 4.2.1, but on another Phone with 4.4.2 the Fading occurs.
I've also had a look at the SoundPool, but it misses the Feature of letting me know if the File is still playing.
Am I able to switch off the automatic fade-in or do I need to use the Soundpool and keep an eye on the length of the Track on my own?
Thanks,
VanDahken
You can try to setAudioAttributes(AudioAttributes attributes) before start playing.
atrs = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
Try to play with CONTENT_TYPE.
I have the same issue. The good thing is that it worked on a couple other Android versions, so hopefully only 4.4.2 has the problem. My first solution I discovered was to use an uncompressed music file... (.wav with data format LEI16) but the music file was way too big.
I also found that the fade-in doesn't seem to happen if you set the audio stream type to STREAM_RING but that's probably not a good idea since it's meant for phone rings.
For my app I only have one critical spot (when the app first starts) where the music has to play without the fade-in, and I was able to get a fix for that. The intro takes a few seconds before the music starts, so what I am doing is:
-prepareAsync() at the start of the intro
-then when prepared, setVolume() to 0f and start()
-then, after a small delay (for my case that ends up being a few seconds: the time it takes for the app's intro to complete), add a seek listener, and seekTo(0)
-when the seek completes, set the volume to the desired value
I have an app which uses the ACTION_MEDIA_BUTTON intent with a BroadcastReceiver to control a music player. The user pushes a button on external hardware and it controls the in-app music player. The user can also HOLD DOWN the button and change the volume.
I recently downloaded another app which uses the headset button, and it takes over the media button intent from my app! So when this other app is open and I press the button, the other app will start running, but my app will think that the button is still pressed down so it will cycle the volume.
To summarize,
my app is open, supposed to be sole listener of media button intents
other app gets opened, it also wants to be sole listener of media button intents
button gets pressed with both apps open, control goes to other app
my app thinks the button is being held down, as it lost control as the button was pressed in down mode (I think). It then launches functions I don't want launched because it thinks the user has held down the button.
Is there any way I could make sure that while my app is open it's the sole receiver of this media button intent? Could I at least check to see if another app has taken over, so I can prevent unexpected behaviour?
Thank you for any help, I've never had apps not play nicely before!
You can alter your BroadcastReceiver's priority (make it something large, like 10000): it should then get the Intent first, and then you can pass it on to the other app.
I have a similiar issue. I believe, outside of the 'arms race' over the priorites mentioned, the only real solution is to close the other application. If you are releasing this application to other users, you could possibly give them a message telling them to close other media player apps and services.
In your manifest you can set the intent priority to the max value of an integer which is: 2147483647.
You should not however set your IntentFilter priority over 1000 as it tells you in the API docs. You can set the IntentFilter priority like so:
myIntentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
I assume you should subtract one from the SYSTEM_HIGH_PRIORITY constant as the docs say the value must be less than SYSTEM_HIGH_PRIORITY.
Quote from docs about IntentFilter.setPriority(int):
Applications must use a value that is larger than SYSTEM_LOW_PRIORITY and smaller than SYSTEM_HIGH_PRIORITY.