|
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;
|
@@ -57,6 +60,7 @@ public class UnityNotificationManager extends BroadcastReceiver {
|
57 | 60 | protected static final String KEY_NOTIFICATION_ID = "com.unity.NotificationID";
|
58 | 61 | protected static final String KEY_SMALL_ICON = "smallIcon";
|
59 | 62 | protected static final String KEY_CHANNEL_ID = "channelID";
|
| 63 | + protected static final String SHOW_IN_FOREGROUND = "com.unity.showInForeground"; |
60 | 64 |
|
61 | 65 | protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS = "UNITY_NOTIFICATIONS";
|
62 | 66 | protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS_KEY = "ChannelIDs";
|
@@ -677,7 +681,10 @@ public void onReceive(Context context, Intent intent) {
|
677 | 681 |
|
678 | 682 | // Call the system notification service to notify the notification.
|
679 | 683 | protected static void notify(Context context, int id, Notification notification) {
|
680 |
| - getNotificationManager(context).notify(id, notification); |
| 684 | + boolean showInForeground = notification.extras.getBoolean(SHOW_IN_FOREGROUND, false); |
| 685 | + if (!isInForeground() || showInForeground) { |
| 686 | + getNotificationManager(context).notify(id, notification); |
| 687 | + } |
681 | 688 |
|
682 | 689 | try {
|
683 | 690 | mNotificationCallback.onSentNotification(notification);
|
@@ -802,6 +809,12 @@ public static String getNotificationChannelId(Notification notification) {
|
802 | 809 |
|
803 | 810 | return null;
|
804 | 811 | }
|
| 812 | + |
| 813 | + private static boolean isInForeground() { |
| 814 | + ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo(); |
| 815 | + ActivityManager.getMyMemoryState(appProcessInfo); |
| 816 | + return (appProcessInfo.importance == IMPORTANCE_FOREGROUND || appProcessInfo.importance == IMPORTANCE_VISIBLE); |
| 817 | + } |
805 | 818 |
|
806 | 819 | public static Notification getNotificationFromIntent(Context context, Intent intent) {
|
807 | 820 | Object notification = getNotificationOrBuilderForIntent(context, intent);
|
|
0 commit comments