Skip to content

Commit 775c21e

Browse files
committed
Avoid excessive JNI calls
1 parent c7ea476 commit 775c21e

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -864,13 +864,18 @@ public static AndroidJavaObject CreateNotificationBuilder(int id, AndroidNotific
864864
Debug.LogError("Failed to schedule notification, it did not contain a valid FireTime");
865865
}
866866

867+
// NOTE: JNI calls are expensive, so we avoid calls that set something that is also a default
868+
867869
var notificationBuilder = s_Jni.NotificationManager.CreateNotificationBuilder(channelId);
868870
s_Jni.NotificationManager.SetNotificationIcon(notificationBuilder, s_Jni.NotificationManager.KEY_SMALL_ICON, notification.SmallIcon);
869871
if (!string.IsNullOrEmpty(notification.LargeIcon))
870872
s_Jni.NotificationManager.SetNotificationIcon(notificationBuilder, s_Jni.NotificationManager.KEY_LARGE_ICON, notification.LargeIcon);
871-
s_Jni.NotificationBuilder.SetContentTitle(notificationBuilder, notification.Title);
872-
s_Jni.NotificationBuilder.SetContentText(notificationBuilder, notification.Text);
873-
s_Jni.NotificationBuilder.SetAutoCancel(notificationBuilder, notification.ShouldAutoCancel);
873+
if (!string.IsNullOrEmpty(notification.Title))
874+
s_Jni.NotificationBuilder.SetContentTitle(notificationBuilder, notification.Title);
875+
if (!string.IsNullOrEmpty(notification.Text))
876+
s_Jni.NotificationBuilder.SetContentText(notificationBuilder, notification.Text);
877+
if (notification.ShouldAutoCancel)
878+
s_Jni.NotificationBuilder.SetAutoCancel(notificationBuilder, notification.ShouldAutoCancel);
874879
if (notification.Number >= 0)
875880
s_Jni.NotificationBuilder.SetNumber(notificationBuilder, notification.Number);
876881
if (notification.Style == NotificationStyle.BigTextStyle)
@@ -889,12 +894,15 @@ public static AndroidJavaObject CreateNotificationBuilder(int id, AndroidNotific
889894
s_Jni.NotificationBuilder.SetGroupSummary(notificationBuilder, notification.GroupSummary);
890895
if (!string.IsNullOrEmpty(notification.SortKey))
891896
s_Jni.NotificationBuilder.SetSortKey(notificationBuilder, notification.SortKey);
892-
s_Jni.NotificationBuilder.SetShowWhen(notificationBuilder, notification.ShowTimestamp);
897+
if (notification.ShowTimestamp)
898+
s_Jni.NotificationBuilder.SetShowWhen(notificationBuilder, notification.ShowTimestamp);
893899
int color = notification.Color.ToInt();
894900
if (color != 0)
895901
s_Jni.NotificationManager.SetNotificationColor(notificationBuilder, color);
896-
s_Jni.NotificationManager.SetNotificationUsesChronometer(notificationBuilder, notification.UsesStopwatch);
897-
s_Jni.NotificationManager.SetNotificationGroupAlertBehavior(notificationBuilder, (int)notification.GroupAlertBehaviour);
902+
if (notification.UsesStopwatch)
903+
s_Jni.NotificationManager.SetNotificationUsesChronometer(notificationBuilder, notification.UsesStopwatch);
904+
if (notification.GroupAlertBehaviour != GroupAlertBehaviours.GroupAlertAll) // All is default value
905+
s_Jni.NotificationManager.SetNotificationGroupAlertBehavior(notificationBuilder, (int)notification.GroupAlertBehaviour);
898906

899907
using (var extras = s_Jni.NotificationBuilder.GetExtras(notificationBuilder))
900908
{

0 commit comments

Comments
 (0)