set custom Sound from only my application in push notification android - java

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?

Related

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!

Android: How to make notification visible even after app is cleared from memory

I want to make a notification which cannot be cancelled by the user manually, just like google navigation notification. I don't know how to do it.
Please Help.
Below is my code that I had tried.
mBuilder.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setContentIntent(pendIntent)
.setContent(mRemoteViews)
.setPriority(0)
.setTicker(ticker);
Thanks.
Try adding this
mBuilder.setOngoing(true)
Other option is to use intentService or background service to make your notification for you instead of the application context.

Android notification, when pushed should open that activity

Im having problems with notifications in android. Im making a chat client.
when my client tells me that someone is chatting with me from a previous chat, and I push it, it opens up a new instance of that chat. I want it to work like the facebook mesenger, that is when I push the notification it opens up the chat for the person which is trying to speak to me, instead of an empty chat window.
regards.
In your Notification you can set a Intent:
Notification n;
n.setContentIntent(intent)
In your intent you will set the activity that will be started and also you can put extras, like an ID of the conversation or something that can be identified latter.
Because when you start this new chat activity on onCreate() you need to check if there is extras, take the ID for example and search for your conversation history and write all your logic.

Running Android App in background or as a constant notification

I'm learning how to make apps for android and I have started by creating one which makes my phone scream when its dropped.
I got it working to where the phone screams when dropped, but now I need to make it so that the phone screams when dropped even when the app is closed, and to show a notification in the notification bar saying that its running
What should I use to do this? Should I use intentService? Ive been looking all over and I'm not sure where to look. Any guides would be appreciated
You need to make your service run in the foreground. You can achieve this by showing a notification when your service is running.
This is how you need to make your service run as foreground
private void showNotification(String title)
{
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title);
startForeground(1000,mBuilder.build()); // 1000 - is Id for the notification
}
You can also set your custom RemoteViews in notification using setContent
You can remove the service from foreground state using stopForeground
check Service Training.
For your use case it's important that it's an foreground service:
Documentation Service

Android SMS API

I know that the SMS content provider is not part of the public API (at least not documented), but if I understand correctly it's still possible to use many of the SMS features as long as you know how to use the API(?).
E.g it's pretty straightforward to insert an SMS into your inbox:
ContentValues values = new ContentValues();
values.put("address", "+457014921911");
contentResolver.insert(Uri.parse("content://sms"), values);
Unfortunately this does not trigger the standard "new-SMS-in-your-inbox" notification. Is it possible to trigger this manually?
Edit: AFAIK the "standard mail application (Messaging)" in Android is listening for incoming SMSes using the android.permission.RECEIVE_SMS permission. And then, when a new SMS has arrived, a status bar notification is inserted with a "special" notification id.
So one solution to my problem (stated above) could be to find, and send the correct broadcast intent; something like "NEW SMS HAS ARRIVED"-intent.
Edit: Downloaded a third party messaging application (chompsms) from Android market. This application satisfies my needs better. When i execute the code above the chompsms notice the new sms and shows the "standard status bar notification". So I would say that the standard Android Messaging application is not detecting sms properly? Or am I wrong?
Unfortunately the code responsible for these notifications is hidden in the messaging application. The class MessagingNotification has a static method updateAllNotifications that you could call using a PathClassLoader and reflection:
PathClassLoader c = new PathClassLoader("/system/app/Mms.apk", getClassLoader());
Class.forName("com.android.mms.util.ContactInfoCache", true, c)
.getMethod("init", Context.class).invoke(null, context);
Class.forName("com.android.mms.transaction.MessagingNotification", true, c)
.getMethod("updateAllNotifications", Context.class).invoke(null, context);
This is obviously a very bad idea for several reasons but I can't think of another way to do what you described.
Could you trigger a PUSH notification after the SMS?
Thread: Does Android support near real time push notification?
Maybe you should replace
content://sms
with
content://sms/inbox

Categories

Resources