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);
Related
the email app is not opening on my android device. there are no error in code but don't know why app isn't working
You are using an old or deprecated way to start activity for ACTION_SEND scenarios. Use below snippet for the correct way.
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)
You can check out the code snippet and detailed explanation here.
https://developer.android.com/training/sharing/send#using-android-system-sharesheet
Starting with API level 30, if you're targeting that version or higher, your app cannot see, or directly interact with, most external packages without explicitly requesting allowance.
Check this answer, it works for me.
https://stackoverflow.com/a/65166064/7248394
So I am making an app that has to check for some information, but I need to be able to do that while the app is killed/not actively running. So basically like Youtube's notifications or something. I am a beginner and watched some tutorials on FCM, which can send a notification while the app is killed, which is fine. However, I need to be able to periodically make API calls, check if a certain condition is true and send the notification if so (all of that while app is not running). I tried googling that and found nothing that can help me. So... any ideas? (No code included since I don't think it's relevant.)
If this condition only affects the display of notifications, you can simply check the condition when you receive a notification through FCM. Here's an example :
class FCMService : FirebaseMessagingService()
{
override fun onMessageReceived(message: RemoteMessage)
{
val myCondition = getConditionFromAPI()
if (myCondition) {
showNotification(message)
}
}
}
Use AlarmManager to request periodic runs of your code. It may be set up to broadcast a message time to time to your receiver (declared in AndroidManifest.xml or in your code).
It will work while device is on. To continue after reboot, you have to set up a receiver for Intent.ACTION_BOOT_COMPLETED and register it in your code (not in AndroidManifest.xml, as it no longer works in latest versions of Android).
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.
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
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