Two notifications from MediaSessionCompat - android

My app can play music from the internet. For control, I use a push notification with buttons for pause/play and rewind. I create it via NotificationCompat Builder and set the MediaStyle. On the lock screen, MediaSessionCompat displays a full-screen notification for monitoring. But there is also a duplicate of the first notification. Because of this, I have two notifications on the lock screen.
Perhaps the MediaSession itself should hide the notification by its id or channel? I tried setting VISIBILITY_SECRET to the channel and notification, but it also displayed on the lock screen.

This may be behavior specific to your phone; I would first start by checking what the behavior is on an emulator.
Perhaps you'd like to compare how you're registering your MediaSession in the notification against the Universal Android Music Player Sample. In particular, check if you've set the MediaSession token with the MediaStyle notification using setMediaSession(); you can find how this is accomplished inside UAMP inside UampNotificationManager.

Related

Set Firebase Notification to appear as Popup like whatsapp in Android studio

I am building an social media app in Android studio, this app uses firebase for push notifications, and notifications is working fine, that Is notifications shows on device's notification bar.
How can I change the behavior so that the notification can appear as popup when app is in foreground?
I want a persistent kind of notification, something like WhatsApp that can stay on top other apps so that users can quickly open notification on wherever they are on their phone.
On my research, the best i could get was to use this on my theme
But I do not have Theme.Holo.Dialog in my style and my aim is that the notification should appear even if app is not open.
Thank you for looking into this
You should use heads-up notifications
For this purposes you should use notification channel with high a importance: How to set importance in the notification channel.
In this answer you can find code sample, how to show notification in the channel with high importance(to show notification even if the app in the foreground):
https://stackoverflow.com/a/67953864/16210149
Hope it would be helpful.

Google cast v3 notification and foreground service notification

I am using ExoPlayer in foreground service together with MediaStyle notification and MediaSessionToken to control playback. I also want to be able to Cast the content to Google Cast receiver. This could be the normal use case for every music player.
The cast is creating its own notification so I have 2 notifications to control the same playback which is bad UX.
I have found this SO question link to disable the default cast notification by setting .setNotificationOptions(null). The problem is that the system creates another notification "A device on your Wi-Fi is casting" as seen on the screenshot. This is the notification that is shown on every device in the same network.
My question is: Is it possible to share the same notification for foreground service and cast service?
Or at least tell the system that this is the device that is actually sending the cast, so it won't show the silent system notification "A device on your Wi-Fi is casting" on that particular device?
Thank you.

MediaStyle Notification not working properly

I want to create a MediaStyle Notification. I followed the official guide but I am facing some problems.
The notification is shown when I am starting the playback service as a foreground but
the play/pause button does nothing
the content activity is not shown if you click the notification
the playback service is not stopped when a swipe gesture happens
How can I solve this? Here is my MediaBrowserServiceCompat class. The notification is shown via showNotification() in onPlay().

How to turn off and turn default notification sound and vibrations in android programmatically

I am trying to turn off and turn on the default notification sound and vibration that is triggered when I receive a notification on my mobile. I am trying to achieve a certain functionality before which I turn off the defaults and after whose completion I would restore the defaults.
Currently I tried setting the STREAM_NOTIFICATION volume to zero and restoring it upon receiving the notification, but it does not work intermittently in some cases.
Is this possible using the Notification class? I am interested in turning off and turning on the default notification sound and vibrate pattern only.
Explanation with code would be greatly appreciated.
There is one very simple solution for it Create one activity for app setting and in your app in which one check box(toggle button) for mute and unmute notification and on button click
getApp().getPreferences().setIsNotitficationMute(isChecked);
and while you are creating notification i.e. in firebase add these code.
if (app.getPreferences().getIsNotitficationMute()) {
notificationSound = null;
} else {
notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}

How to receive Parse push notification only when app is opened, and show it in modal dialog form

I was using https://github.com/ParsePlatform/PushTutorial sample code for a while.
Current example behavior is
Push notification will be received, either the app is opened or closed.
Push notification is in the form of showing icon in status bar with sound.
I cannot find a way to specific notification icon image. Parse is using app icon as notification icon by default. By Android design guideline requires us to have different style and size for notification icon.
When the app is opened, and notification received. Clicking on the notification icon will cause 2nd instance of the same app is launched. That's mean, there will be 2 instances of same app. (Personally, I don't feel this is a correct behavior)
I was wondering
How can I only receive the push notification when app is opened, but not closed?
How can I show it in the form of modal dialog, instance of showing icon in status bar with sound?
I am not sure about the 4th point, it didn't work that way for me. It opened the same instance for me. I think you are using a different application identifier and have two different application with same name and icon, but different identifier.
Now, for your use case, I think Push notification is not the ideal solution. Depending on what and how frequent you need to show this, you may opt for a repeating pull from the server or if you still want to use Push Notification then subscribe/unsubscribe from a Push Notification Channel when the app is pulled to foreground or background.
ie, when the application is in foreground (onStart() / onResume()), subscribe to a channel:
PushService.subscribe(context, "foregroundPush", YourActivity.class);
and when the application is moving to background (onStop() / onResume() / onDestroy() ), unsubscribe from the same channel:
PushService.unsubscribe(context, "foregroundPush", YourActivity.class);
Whenever you need to send a push notification to the devices with your application in foreground, use the channel 'foregroundPush'

Categories

Resources