Skip to content

Commit b67b19b

Browse files
committed
Remove mirrored notification when dismissed on origin device
1 parent 6974a00 commit b67b19b

File tree

6 files changed

+115
-9
lines changed

6 files changed

+115
-9
lines changed

.idea/deploymentTargetSelector.xml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/noti/main/service/FirebaseMessageService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public interface OnNotificationRemoveRequest {
128128
}
129129

130130
public static OnNotificationRemoveRequest removeListener;
131+
public static OnNotificationRemoveRequest removeListenerById;
131132

132133
@Override
133134
public void onCreate() {
@@ -280,6 +281,7 @@ public void processReception(Map<String, String> map, Context context) {
280281
switch (type) {
281282
case "send|sms" -> sendSmsNotification(map);
282283
case "send|telecom" -> sendTelecomNotification(map);
284+
case "send|dismiss" -> dismissMirroredNotification(map);
283285
case "send|normal" -> {
284286
if(map.containsKey(NotificationRequest.KEY_NOTIFICATION_API)) {
285287
sendNotificationNew(map);
@@ -796,6 +798,17 @@ protected void sendSmsNotification(Map<String, String> map) {
796798
playRingtoneAndVibrate();
797799
}
798800

801+
protected void dismissMirroredNotification(Map<String, String> map) {
802+
if(!map.containsKey(NotificationRequest.KEY_NOTIFICATION_API)) {
803+
return;
804+
}
805+
806+
String key = map.get(NotificationRequest.KEY_NOTIFICATION_KEY);
807+
if(key != null && !key.isEmpty() && removeListenerById != null) {
808+
removeListenerById.onRequested(key);
809+
}
810+
}
811+
799812
protected void sendNotificationAction(Map<String, String> map) {
800813
String key = map.get(NotificationRequest.KEY_NOTIFICATION_KEY);
801814
int actionIndex = Integer.parseInt(map.get(NotificationRequest.KEY_NOTIFICATION_ACTION_INDEX));

app/src/main/java/com/noti/main/service/NotiListenerService.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public class NotiListenerService extends NotificationListenerService {
7676
private static final Object pastNotificationLock = new Object();
7777
private volatile StatusBarNotification pastNotification = null;
7878
private final FirebaseMessageService.OnNotificationRemoveRequest onRemoveRequestListener = this::cancelNotification;
79+
private final FirebaseMessageService.OnNotificationRemoveRequest onRemoveRequestListenerById = key -> {
80+
StatusBarNotification[] notifications = this.getActiveNotifications();
81+
for (StatusBarNotification statusBarNotification : notifications) {
82+
String notificationKey = statusBarNotification.getKey();
83+
if(statusBarNotification.getPackageName().equals(getPackageName()) && notificationKey.contains(Integer.toString(key.hashCode()))) {
84+
cancelNotification(notificationKey);
85+
break;
86+
}
87+
}
88+
};
7989

8090
private static int queryAccessCount = 0;
8191
private static volatile long intervalTimestamp = 0;
@@ -103,6 +113,7 @@ public void onCreate() {
103113
super.onCreate();
104114
if (instance == null) initService(this);
105115
FirebaseMessageService.removeListener = onRemoveRequestListener;
116+
FirebaseMessageService.removeListenerById = onRemoveRequestListenerById;
106117
}
107118

108119
void initService(Context context) {
@@ -263,6 +274,55 @@ public void onListenerConnected() {
263274
LiveNotiProcess.mOnNotificationListListener = NotiListenerService.this::getActiveNotifications;
264275
}
265276

277+
@Override
278+
public void onNotificationRemoved(StatusBarNotification sbn) {
279+
super.onNotificationRemoved(sbn);
280+
if(BuildConfig.DEBUG) {
281+
Log.d("NotificationListener", "onNotificationRemoved: " + sbn.getPackageName());
282+
}
283+
284+
if (manager == null) manager = PowerUtils.getInstance(this);
285+
manager.acquire();
286+
287+
if(!prefs.getBoolean("RemoteDismiss", false)) {
288+
manager.release();
289+
return;
290+
}
291+
292+
if (!prefs.getString("UID", "").isEmpty() && prefs.getBoolean("serviceToggle", false)) {
293+
String mode = prefs.getString("service", "reception");
294+
if (mode.equals("send") || mode.equals("hybrid")) {
295+
Notification notification = sbn.getNotification();
296+
String PackageName = sbn.getPackageName();
297+
String TITLE = getNonNullString(notification.extras.getCharSequence(Notification.EXTRA_TITLE));
298+
299+
if (PackageName.equals(getPackageName()) && (!TITLE.toLowerCase().contains("test") || TITLE.contains("main"))) {
300+
manager.release();
301+
} else if (isWhitelist(PackageName)) {
302+
if (prefs.getBoolean("IgnoreOngoing", false) &&
303+
(notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0 ||
304+
(notification.flags & Notification.FLAG_ONGOING_EVENT) != 0 ||
305+
(notification.flags & Notification.FLAG_LOCAL_ONLY) != 0) {
306+
manager.release();
307+
return;
308+
}
309+
310+
if (notification.contentView != null &&
311+
notification.contentView.getLayoutId() == androidx.media.R.layout.notification_template_media) {
312+
manager.release();
313+
return;
314+
}
315+
316+
if (prefs.getBoolean("useLegacyAPI", false)) {
317+
manager.release();
318+
} else {
319+
NotificationRequest.sendOriginNotificationDismissed(this, BuildConfig.DEBUG, sbn.getKey());
320+
}
321+
}
322+
}
323+
}
324+
}
325+
266326
@Override
267327
public void onNotificationPosted(StatusBarNotification sbn) {
268328
super.onNotificationPosted(sbn);

app/src/main/java/com/noti/main/service/mirnoti/NotificationActionProcess.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
import android.service.notification.StatusBarNotification;
1414
import android.util.Log;
1515

16+
import androidx.annotation.NonNull;
1617
import androidx.annotation.Nullable;
17-
1818
import org.jetbrains.annotations.TestOnly;
1919

20+
import com.noti.main.service.FirebaseMessageService;
21+
2022
import java.util.concurrent.ConcurrentHashMap;
2123

2224
public class NotificationActionProcess {
@@ -114,6 +116,7 @@ public static class NotificationActionRaiseBroadcastReceiver extends BroadcastRe
114116
@Override
115117
public void onReceive(Context context, Intent intent) {
116118
if(intent.hasExtra(TEST_INPUT_ACTION_KET)) {
119+
// For testing purposes only; must do nothing on other packages
117120
notifyTestInputAction(context, intent);
118121
return;
119122
}
@@ -133,13 +136,17 @@ public void onReceive(Context context, Intent intent) {
133136

134137
final int uniqueCode = intent.getIntExtra(NotificationRequest.KEY_NOTIFICATION_HASHCODE, -1);
135138
if(uniqueCode != -1) {
136-
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
137-
notificationManager.cancel(uniqueCode);
139+
if(FirebaseMessageService.removeListenerById == null) {
140+
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
141+
notificationManager.cancel(uniqueCode);
142+
} else {
143+
FirebaseMessageService.removeListenerById.onRequested(key);
144+
}
138145
}
139146
}
140147

141148
@TestOnly
142-
protected void notifyTestInputAction(Context context, Intent intent) {
149+
protected void notifyTestInputAction(@NonNull Context context, Intent intent) {
143150
Log.d("ReplyData", "InputType action receiver: " + RemoteInput.getResultsFromIntent(intent).get(intent.getStringExtra(TEST_INPUT_ACTION_KET)));
144151
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
145152
notificationManager.cancel(1111);

app/src/main/java/com/noti/main/service/mirnoti/NotificationRequest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ public static void sendMirrorNotification(Context context, boolean isLogging, St
5454
}
5555
}
5656

57+
public static void sendOriginNotificationDismissed(Context context, boolean isLogging, String notificationKey) {
58+
try {
59+
JSONObject notificationBody = new JSONObject();
60+
notificationBody.put("type", "send|dismiss");
61+
notificationBody.put("device_name", NotiListenerService.getDeviceName());
62+
notificationBody.put("device_id", NotiListenerService.getUniqueID());
63+
notificationBody.put(KEY_NOTIFICATION_API, "1");
64+
notificationBody.put(KEY_NOTIFICATION_KEY, notificationKey);
65+
66+
if (isLogging) Log.d("NOTIFICATION_DATA", notificationBody.toString());
67+
NotiListenerService.sendNotification(notificationBody, "NOTIFICATION_SEND", context);
68+
} catch (JSONException e) {
69+
if(isLogging) {
70+
throw new RuntimeException(e);
71+
}
72+
}
73+
}
74+
5775
public static void sendPerformAction(Context context, String key, int index, String deviceName, String deviceId) {
5876
try {
5977
JSONObject notificationBody = new JSONObject();

app/src/main/java/com/noti/main/ui/options/OtherPreference.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static android.content.Context.MODE_PRIVATE;
44

55
import android.app.Activity;
6+
import android.app.NotificationChannel;
67
import android.app.NotificationManager;
78
import android.app.PendingIntent;
89
import android.content.Context;
@@ -359,8 +360,6 @@ public boolean onPreferenceTreeClick(Preference preference) {
359360
.setLargeIcon(R.drawable.ic_launcher_splash)
360361
.setPicture(R.drawable.ic_launcher_background)
361362
.setSmallIcon(R.drawable.ic_broken_image)
362-
.setChannelName("Testing Channel")
363-
.setChannelId("Notification Test")
364363
.setImportance(importance)
365364
.enableVibration(true)
366365
.setAutoCancel(true)
@@ -378,6 +377,15 @@ public boolean onPreferenceTreeClick(Preference preference) {
378377
.setContentText("messageTest");
379378

380379
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
380+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
381+
CharSequence channelName = "Testing Channel";
382+
String description = "Test Notification Channel";
383+
384+
NotificationChannel channel = new NotificationChannel("Notification Test", channelName, importance.ordinal());
385+
channel.setDescription(description);
386+
notificationManager.createNotificationChannel(channel);
387+
}
388+
381389
notificationManager.notify(1111, builder.build());
382390
break;
383391

0 commit comments

Comments
 (0)