|
1 | 1 | package com.unity.androidnotifications;
|
2 | 2 |
|
3 | 3 | import android.app.Activity;
|
| 4 | +import android.app.ActivityManager; |
4 | 5 | import android.app.AlarmManager;
|
5 | 6 | import android.app.Notification;
|
6 | 7 | import android.app.NotificationManager;
|
|
21 | 22 | import android.service.notification.StatusBarNotification;
|
22 | 23 | import android.util.Log;
|
23 | 24 |
|
| 25 | +import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 26 | +import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
24 | 27 | import static android.app.Notification.VISIBILITY_PUBLIC;
|
25 | 28 |
|
26 | 29 | import java.lang.Integer;
|
@@ -53,6 +56,7 @@ public class UnityNotificationManager extends BroadcastReceiver {
|
53 | 56 | protected static final String KEY_NOTIFICATION = "unityNotification";
|
54 | 57 | protected static final String KEY_SMALL_ICON = "smallIcon";
|
55 | 58 | protected static final String KEY_CHANNEL_ID = "channelID";
|
| 59 | + protected static final String SHOW_IN_FOREGROUND = "showInForeground"; |
56 | 60 |
|
57 | 61 | protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS = "UNITY_NOTIFICATIONS";
|
58 | 62 | protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS_KEY = "ChannelIDs";
|
@@ -601,7 +605,10 @@ protected static void sendNotification(Context context, Intent intent) {
|
601 | 605 |
|
602 | 606 | // Call the system notification service to notify the notification.
|
603 | 607 | protected static void notify(Context context, int id, Notification notification) {
|
604 |
| - getNotificationManager(context).notify(id, notification); |
| 608 | + boolean showInForeground = notification.extras.getBoolean(SHOW_IN_FOREGROUND, false); |
| 609 | + if (!isInForeground() || showInForeground) { |
| 610 | + getNotificationManager(context).notify(id, notification); |
| 611 | + } |
605 | 612 |
|
606 | 613 | try {
|
607 | 614 | mNotificationCallback.onSentNotification(notification);
|
@@ -734,6 +741,12 @@ public static String getNotificationChannelId(Notification notification) {
|
734 | 741 |
|
735 | 742 | return null;
|
736 | 743 | }
|
| 744 | + |
| 745 | + private static boolean isInForeground() { |
| 746 | + ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo(); |
| 747 | + ActivityManager.getMyMemoryState(appProcessInfo); |
| 748 | + return (appProcessInfo.importance == IMPORTANCE_FOREGROUND || appProcessInfo.importance == IMPORTANCE_VISIBLE); |
| 749 | + } |
737 | 750 |
|
738 | 751 | public void showNotificationSettings(String channelId) {
|
739 | 752 | Intent settingsIntent;
|
|
0 commit comments