Skip to content

Commit 80c7231

Browse files
authored
Merge pull request #207 from Unity-Technologies/fix-recovering-builder
Fix recovering builder
2 parents 7a7b942 + 11f05d6 commit 80c7231

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -524,18 +524,23 @@ protected static Class<?> getOpenAppActivity(Context context, boolean fallbackTo
524524
}
525525

526526
protected static Notification.Builder recoverBuilder(Context context, Notification notification) {
527-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
528-
Notification.Builder builder = Notification.Builder.recoverBuilder(context, notification);
529-
// extras not recovered, transfer manually
530-
builder.setExtras(notification.extras);
531-
return builder;
532-
}
533-
else {
534-
return recoverBuilderPreNougat(context, notification);
527+
try {
528+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
529+
Notification.Builder builder = Notification.Builder.recoverBuilder(context, notification);
530+
// extras not recovered, transfer manually
531+
builder.setExtras(notification.extras);
532+
return builder;
533+
}
534+
} catch (Exception e) {
535+
Log.e(TAG_UNITY, "Failed to recover builder for notification!", e);
536+
} catch (OutOfMemoryError e) {
537+
Log.e(TAG_UNITY, "Failed to recover builder for notification!", e);
535538
}
539+
540+
return recoverBuilderCustom(context, notification);
536541
}
537542

538-
private static Notification.Builder recoverBuilderPreNougat(Context context, Notification notification) {
543+
private static Notification.Builder recoverBuilderCustom(Context context, Notification notification) {
539544
String channelID = notification.extras.getString(KEY_CHANNEL_ID);
540545
Notification.Builder builder = UnityNotificationManager.createNotificationBuilder(context, channelID);
541546
UnityNotificationManager.setNotificationIcon(builder, KEY_SMALL_ICON, notification.extras.getString(KEY_SMALL_ICON));

com.unity.mobile.notifications/Tests/Runtime/Android/AndroidNotificationSimpleTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public void LegacyRecoverBuilderProducesTheSameNotification()
472472
var manager = managerClass.GetStatic<AndroidJavaObject>("mUnityNotificationManager");
473473
var context = manager.Get<AndroidJavaObject>("mContext");
474474
var utils = new AndroidJavaClass("com.unity.androidnotifications.UnityNotificationUtilities");
475-
var recoveredBuilder = utils.CallStatic<AndroidJavaObject>("recoverBuilderPreNougat", context, notification);
475+
var recoveredBuilder = utils.CallStatic<AndroidJavaObject>("recoverBuilderCustom", context, notification);
476476
Assert.IsNotNull(recoveredBuilder);
477477

478478
var notificationAfterRecover = recoveredBuilder.Call<AndroidJavaObject>("build");

0 commit comments

Comments
 (0)