Skip to content

Commit 76d8b25

Browse files
committed
Add test for deserializing v0 serialized notification
1 parent 8b68f3c commit 76d8b25

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,53 @@ private static Notification.Builder recoverBuilderPreNougat(Context context, Not
566566

567567
return builder;
568568
}
569+
570+
// for testing purposes: copy-paste of what serialization was in version 0
571+
// except for hardcoded version 0
572+
private static boolean serializeNotificationCustom_v0(Notification notification, DataOutputStream out) {
573+
try {
574+
out.write(UNITY_MAGIC_NUMBER);
575+
out.writeInt(0); // NOTIFICATION_SERIALIZATION_VERSION
576+
577+
// serialize extras
578+
boolean showWhen = notification.extras.getBoolean(Notification.EXTRA_SHOW_WHEN, false);
579+
byte[] extras = serializeParcelable(notification.extras);
580+
out.writeInt(extras == null ? 0 : extras.length);
581+
if (extras != null && extras.length > 0)
582+
out.write(extras);
583+
else {
584+
// parcelling may fail in case it contains binder object, when that happens serialize manually what we care about
585+
out.writeInt(notification.extras.getInt(KEY_ID));
586+
serializeString(out, notification.extras.getString(Notification.EXTRA_TITLE));
587+
serializeString(out, notification.extras.getString(Notification.EXTRA_TEXT));
588+
serializeString(out, notification.extras.getString(KEY_SMALL_ICON));
589+
serializeString(out, notification.extras.getString(KEY_LARGE_ICON));
590+
out.writeLong(notification.extras.getLong(KEY_FIRE_TIME, -1));
591+
out.writeLong(notification.extras.getLong(KEY_REPEAT_INTERVAL, -1));
592+
serializeString(out, Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? null : notification.extras.getString(Notification.EXTRA_BIG_TEXT));
593+
out.writeBoolean(notification.extras.getBoolean(Notification.EXTRA_SHOW_CHRONOMETER, false));
594+
out.writeBoolean(showWhen);
595+
serializeString(out, notification.extras.getString(KEY_INTENT_DATA));
596+
}
597+
598+
serializeString(out, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? null : notification.getChannelId());
599+
Integer color = UnityNotificationManager.getNotificationColor(notification);
600+
out.writeBoolean(color != null);
601+
if (color != null)
602+
out.writeInt(color);
603+
out.writeInt(notification.number);
604+
out.writeBoolean(0 != (notification.flags & Notification.FLAG_AUTO_CANCEL));
605+
serializeString(out, notification.getGroup());
606+
out.writeBoolean(0 != (notification.flags & Notification.FLAG_GROUP_SUMMARY));
607+
out.writeInt(UnityNotificationManager.getNotificationGroupAlertBehavior(notification));
608+
serializeString(out, notification.getSortKey());
609+
if (showWhen)
610+
out.writeLong(notification.when);
611+
612+
return true;
613+
} catch (Exception e) {
614+
Log.e(TAG_UNITY, "Failed to serialize notification", e);
615+
return false;
616+
}
617+
}
569618
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,30 @@ public void CorruptedPrimarySerialization_FallsBack()
475475
Assert.AreEqual(original.SmallIcon, deserializedData.Notification.SmallIcon);
476476
Assert.AreEqual(original.LargeIcon, deserializedData.Notification.LargeIcon);
477477
}
478+
479+
[Test]
480+
[UnityPlatform(RuntimePlatform.Android)]
481+
public void CanDeserializeCustomSerializedNotification_v0()
482+
{
483+
const int notificationId = 245;
484+
485+
var original = CreateNotificationWithAllParameters();
486+
487+
AndroidNotificationIntentData deserialized;
488+
using (var builder = AndroidNotificationCenter.CreateNotificationBuilder(notificationId, original, kChannelId))
489+
{
490+
// put something to extrax to force completely custom serialization of them
491+
var bitmap = CreateBitmap();
492+
Assert.IsNotNull(bitmap);
493+
var extras = builder.Call<AndroidJavaObject>("getExtras");
494+
extras.Call("putParcelable", "binder_item", bitmap);
495+
496+
// Serialize like we did in version 0
497+
deserialized = SerializeDeserializeNotification(builder, "serializeNotificationCustom_v0");
498+
}
499+
500+
Assert.IsNotNull(deserialized);
501+
original.ShowInForeground = true; // v0 did not have this, so should default to true
502+
CheckNotificationsMatch(original, deserialized.Notification);
503+
}
478504
}

0 commit comments

Comments
 (0)