-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7111543
commit 03fd93d
Showing
5 changed files
with
70 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
android/app/src/main/java/app/getbaseline/baseline/NotificationWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package app.getbaseline.baseline; | ||
|
||
import android.app.NotificationManager; | ||
import android.content.Context; | ||
import android.service.notification.StatusBarNotification; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.work.Worker; | ||
import androidx.work.WorkerParameters; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class NotificationWorker extends Worker { | ||
public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { | ||
super(context, workerParams); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Result doWork() { | ||
// For some reason, you can only pull delivered notifications at Android M, | ||
// so it's only worth running any of this if we're at that SDK level | ||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { | ||
// Find the latest notification in all delivered notifications | ||
NotificationManager notificationManager = getApplicationContext().getSystemService(NotificationManager.class); | ||
ArrayList<StatusBarNotification> notifs = new ArrayList<>(); | ||
StatusBarNotification latestNotif = null; | ||
for (StatusBarNotification notif : notificationManager.getActiveNotifications()) { | ||
notifs.add(notif); | ||
if (latestNotif == null || latestNotif.getPostTime() < notif.getPostTime()) { | ||
latestNotif = notif; | ||
} | ||
} | ||
|
||
// Remove the latest from the list of all delivered | ||
if (latestNotif != null) { | ||
notifs.remove(latestNotif); | ||
} | ||
|
||
// And remove all other notifications | ||
for (StatusBarNotification notif : notifs) { | ||
notificationManager.cancel(notif.getTag(), notif.getId()); | ||
} | ||
} | ||
|
||
return Result.success(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.