Skip to content

Commit bfacfa9

Browse files
committed
Add ShowInForeground boolean feature for Android notifications.
- Android notification pop-ups are shown even when app is on the foreground if it is received on a notification channel with high priority - With ShowInForeground option, it is possible not to show notification pop-ups if app is on the foreground - Related callbacks about receiving notification will still be called
1 parent 34f0ad4 commit bfacfa9

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

com.unity.mobile.notifications/Runtime/Android/AndroidNotification.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ public DateTime CustomTimestamp
177177
}
178178
}
179179

180+
/// <summary>
181+
/// Set this notification to be shown when app is in the foreground.
182+
/// </summary>
183+
public bool ShowInForeground { get; set; }
184+
180185
internal bool ShowCustomTimestamp { get; set; }
181186

182187
private Color m_Color;
@@ -208,6 +213,7 @@ public AndroidNotification(string title, string text, DateTime fireTime)
208213
GroupAlertBehaviour = GroupAlertBehaviours.GroupAlertAll;
209214
ShowTimestamp = false;
210215
ShowCustomTimestamp = false;
216+
ShowInForeground = true;
211217

212218
m_RepeatInterval = (-1L).ToTimeSpan();
213219
m_Color = new Color(0, 0, 0, 0);

com.unity.mobile.notifications/Runtime/Android/AndroidNotificationCenter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class AndroidNotificationCenter
6666
private static AndroidJavaObject KEY_REPEAT_INTERVAL;
6767
private static AndroidJavaObject KEY_NOTIFICATION;
6868
private static AndroidJavaObject KEY_SMALL_ICON;
69+
private static AndroidJavaObject SHOW_IN_FOREGROUND;
6970

7071
/// <summary>
7172
/// Initialize the AndroidNotificationCenter class.
@@ -115,6 +116,7 @@ public static bool Initialize()
115116
KEY_REPEAT_INTERVAL = s_NotificationManagerClass.GetStatic<AndroidJavaObject>("KEY_REPEAT_INTERVAL");
116117
KEY_NOTIFICATION = s_NotificationManagerClass.GetStatic<AndroidJavaObject>("KEY_NOTIFICATION");
117118
KEY_SMALL_ICON = s_NotificationManagerClass.GetStatic<AndroidJavaObject>("KEY_SMALL_ICON");
119+
SHOW_IN_FOREGROUND = s_NotificationManagerClass.GetStatic<AndroidJavaObject>("SHOW_IN_FOREGROUND");
118120

119121
s_Initialized = true;
120122
#endif
@@ -467,6 +469,7 @@ public static AndroidJavaObject CreateNotificationBuilder(int id, AndroidNotific
467469
extras.Call("putInt", KEY_ID, id);
468470
extras.Call("putLong", KEY_REPEAT_INTERVAL, notification.RepeatInterval.ToLong());
469471
extras.Call("putLong", KEY_FIRE_TIME, fireTime);
472+
extras.Call("putBoolean", SHOW_IN_FOREGROUND, notification.ShowInForeground);
470473
if (!string.IsNullOrEmpty(notification.IntentData))
471474
extras.Call("putString", KEY_INTENT_DATA, notification.IntentData);
472475
}
@@ -494,6 +497,7 @@ internal static AndroidNotificationIntentData GetNotificationData(AndroidJavaObj
494497
notification.UsesStopwatch = extras.Call<bool>("getBoolean", Notification_EXTRA_SHOW_CHRONOMETER, false);
495498
notification.FireTime = extras.Call<long>("getLong", KEY_FIRE_TIME, -1L).ToDatetime();
496499
notification.RepeatInterval = extras.Call<long>("getLong", KEY_REPEAT_INTERVAL, -1L).ToTimeSpan();
500+
notification.ShowInForeground = extras.Call<bool>("getBoolean", SHOW_IN_FOREGROUND, false);
497501

498502
if (extras.Call<bool>("containsKey", Notification_EXTRA_BIG_TEXT))
499503
notification.Style = NotificationStyle.BigTextStyle;

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationManager.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.unity.androidnotifications;
22

33
import android.app.Activity;
4+
import android.app.ActivityManager;
45
import android.app.AlarmManager;
56
import android.app.Notification;
67
import android.app.NotificationManager;
@@ -21,6 +22,8 @@
2122
import android.service.notification.StatusBarNotification;
2223
import android.util.Log;
2324

25+
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
26+
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
2427
import static android.app.Notification.VISIBILITY_PUBLIC;
2528

2629
import java.lang.Integer;
@@ -53,6 +56,7 @@ public class UnityNotificationManager extends BroadcastReceiver {
5356
protected static final String KEY_NOTIFICATION = "unityNotification";
5457
protected static final String KEY_SMALL_ICON = "smallIcon";
5558
protected static final String KEY_CHANNEL_ID = "channelID";
59+
protected static final String SHOW_IN_FOREGROUND = "showInForeground";
5660

5761
protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS = "UNITY_NOTIFICATIONS";
5862
protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS_KEY = "ChannelIDs";
@@ -601,7 +605,10 @@ protected static void sendNotification(Context context, Intent intent) {
601605

602606
// Call the system notification service to notify the notification.
603607
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+
}
605612

606613
try {
607614
mNotificationCallback.onSentNotification(notification);
@@ -734,6 +741,12 @@ public static String getNotificationChannelId(Notification notification) {
734741

735742
return null;
736743
}
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+
}
737750

738751
public void showNotificationSettings(String channelId) {
739752
Intent settingsIntent;

0 commit comments

Comments
 (0)