Skip to content

Commit

Permalink
fix(android): auto cancel and close notification drawer when action i…
Browse files Browse the repository at this point in the history
…s pressed (#276)
  • Loading branch information
helenaford authored Jan 6, 2022
1 parent a4ccea2 commit f5da722
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.BROADCAST_CLOSE_SYSTEM_DIALOGS" android:maxSdkVersion="30" />

<application>
<!-- Receiver Service -->
Expand Down
21 changes: 21 additions & 0 deletions android/src/main/java/app/notifee/core/ReceiverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.RemoteInput;
import app.notifee.core.event.InitialNotificationEvent;
import app.notifee.core.event.MainComponentEvent;
import app.notifee.core.event.NotificationEvent;
import app.notifee.core.model.NotificationAndroidModel;
import app.notifee.core.model.NotificationAndroidPressActionModel;
import app.notifee.core.model.NotificationModel;
import app.notifee.core.utility.IntentUtils;
Expand Down Expand Up @@ -168,6 +171,7 @@ private void onActionPressIntent(Intent intent) {
}

NotificationModel notificationModel = NotificationModel.fromBundle(notification);
NotificationAndroidModel notificationAndroidModel = notificationModel.getAndroid();
NotificationAndroidPressActionModel pressActionBundle =
NotificationAndroidPressActionModel.fromBundle(pressAction);

Expand All @@ -184,6 +188,14 @@ private void onActionPressIntent(Intent intent) {

EventBus.post(new NotificationEvent(TYPE_ACTION_PRESS, notificationModel, extras));

if (notificationModel.getAndroid().getAutoCancel()) {
NotificationManagerCompat notificationManagerCompat =
NotificationManagerCompat.from(getApplicationContext());

notificationManagerCompat.cancel(
notificationAndroidModel.getTag(), notificationModel.getId().hashCode());
}

String launchActivity = pressActionBundle.getLaunchActivity();
String mainComponent = pressActionBundle.getMainComponent();

Expand All @@ -195,6 +207,15 @@ private void onActionPressIntent(Intent intent) {
launchActivity,
mainComponent,
pressActionBundle.getLaunchActivityFlags());

int targetSdkVersion = ContextHolder.getApplicationContext().getApplicationInfo().targetSdkVersion;

// Close notification drawer if targetSdkVersion is Android 11 and lower
// See https://developer.android.com/about/versions/12/behavior-changes-all#close-system-dialogs
if (targetSdkVersion < Build.VERSION_CODES.S ) {
ContextHolder.getApplicationContext()
.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
}
}

Expand Down

0 comments on commit f5da722

Please sign in to comment.