Skip to content

Commit 0bad68e

Browse files
committed
Merge branch 'feature/ShowInForeground-feature-for-Android' of https://github.com/innogames/com.unity.mobile.notifications into innogames-feature/ShowInForeground-feature-for-Android
2 parents bfbc1e9 + 10d8020 commit 0bad68e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,21 @@ 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
184+
{
185+
get => !m_SilentInForeground;
186+
set => m_SilentInForeground = !value;
187+
}
188+
180189
internal bool ShowCustomTimestamp { get; set; }
181190

182191
private Color m_Color;
183192
private TimeSpan m_RepeatInterval;
184193
private DateTime m_CustomTimestamp;
194+
private bool m_SilentInForeground;
185195

186196
/// <summary>
187197
/// Create a notification struct with all optional fields set to default values.
@@ -212,6 +222,7 @@ public AndroidNotification(string title, string text, DateTime fireTime)
212222
m_RepeatInterval = (-1L).ToTimeSpan();
213223
m_Color = new Color(0, 0, 0, 0);
214224
m_CustomTimestamp = (-1L).ToDatetime();
225+
m_SilentInForeground = false;
215226
}
216227

217228
/// <summary>

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;
@@ -57,6 +60,7 @@ public class UnityNotificationManager extends BroadcastReceiver {
5760
protected static final String KEY_NOTIFICATION_ID = "com.unity.NotificationID";
5861
protected static final String KEY_SMALL_ICON = "smallIcon";
5962
protected static final String KEY_CHANNEL_ID = "channelID";
63+
protected static final String SHOW_IN_FOREGROUND = "com.unity.showInForeground";
6064

6165
protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS = "UNITY_NOTIFICATIONS";
6266
protected static final String NOTIFICATION_CHANNELS_SHARED_PREFS_KEY = "ChannelIDs";
@@ -677,7 +681,10 @@ public void onReceive(Context context, Intent intent) {
677681

678682
// Call the system notification service to notify the notification.
679683
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+
}
681688

682689
try {
683690
mNotificationCallback.onSentNotification(notification);
@@ -802,6 +809,12 @@ public static String getNotificationChannelId(Notification notification) {
802809

803810
return null;
804811
}
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+
}
805818

806819
public static Notification getNotificationFromIntent(Context context, Intent intent) {
807820
Object notification = getNotificationOrBuilderForIntent(context, intent);

0 commit comments

Comments
 (0)