Thursday, April 19, 2018

Hide notification from Foreground Service on click notification Action

Leave a Comment

I have an Alarm App that have foreground service with a Heads-Up Notification and that notification have two actions where one send an intent to the Service and can open an activity depending on the app configuration.

The problem is that when i click on a action that sends the intent to the service the notification doesn't hide. This not seems to occur when the intent opens a Activity

I don't want a foreground service without a Notification, i just want it to hide it back to the Notification Drawer when the intent is sent to the service

Here is the code:

NotificationCompat.Builder(mAlarmApplication, CHANNEL_ID)             .setSmallIcon(R.drawable.ic_notification_alarm)             .setAutoCancel(false)             .setOngoing(true)             .setVibrate(LongArray(0))             .setContentTitle("Title")             .setContentText("Content")             .addAction(0, dismissActionText, dismissPendingIntent)             .setCategory(NotificationCompat.CATEGORY_ALARM)             .setPriority(NotificationCompat.PRIORITY_MAX)             .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)             .setContentIntent(alarmScreenPendingIntent)             .setFullScreenIntent(alarmScreenPendingIntent, true) 

Here is the link of the app https://play.google.com/store/apps/details?id=com.garageapp.alarmchallenges.

The problem occurs when alarm start and my current solution is to update the old heads up notification with a new one that is not a heads up but the UX is not a good because on Android 8+ the notification new notification pops up aging

4 Answers

Answers 1

Seems like your Notification is bonded with your Service. If so, then you have to kill the notification in Service

Did you try?

public static void cancelNotification(Context ctx, int notifyId) {     String ns = Context.NOTIFICATION_SERVICE;     NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);     nMgr.cancel(notifyId); } 

Answers 2

You are using .setOngoing(true) which should not be removed while service is working.

.setAutoCancel(true) will also not working with .setOngoing(true).

You have to use .setOngoing(false) to dismiss the notification.

Answers 3

If you or user remove your foreground notification your service will go to background, I think that best work is to not using heads up notification for foreground by not setting its priority to MAX

Use two notifications at same time one in drawer and another heads up:

-The first notification with priority DEFAULT for starting foreground ( auto cancel set to false and ongoing set to true) show this one with startForground()

-The Second notification (Heads up (Priority MAX) auto cancel set to true and on going set to false) for your actions show this with notifyManager.notify()

These two notifications must have different IDs


another solution:

If you want to use one heads up notification with actions for foreground service you may do this:

use a heads up notification with your action buttons for foreground service when the user clicks actions this action must call the foreground service and then the foreground service could call startForeground (with same id) with a new notification with priority set to default, if your notification could not be updated you may need to call stopForeground(true) or notificationManager.cancel(id) first before calling startForeground with new notification. both of these two notifications should has on going set to true and auto cancel set to false

In my opinion the first solution is better than the second because the notification may not update in second solution.

Answers 4

As the documentation says :

A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of ...

android system does not allow you to have a foreground service without notification or a hidden notification. and that's because of user awareness of what is happening in his/her system.

also killing the notification will stop your foreground service. so you never can have both of the options (foreground service and hidden notification)

a not clear solution for your problem:

when you call action that sends the intent to the service, do this with a mediator activity i mean first open an activity and in the activity send intent to the service.

I hope this solve your problem as you told :

The problem is that when i click on a action that sends the intent to the service the notification doesn't hide. This not seems to occur when the intent opens a Activity

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment