Skip to content

Commit 8fe97b1

Browse files
committed
Make a copy of current serialization
1 parent 9f98666 commit 8fe97b1

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class UnityNotificationUtilities {
4545
private static final int NOTIFICATION_SERIALIZATION_VERSION = 1;
4646
private static final int INTENT_SERIALIZATION_VERSION = 0;
4747

48-
private static final String SAVED_NOTIFICATION_PRIMARY_KEY = "data";
49-
private static final String SAVED_NOTIFICATION_FALLBACK_KEY = "fallback.data";
48+
static final String SAVED_NOTIFICATION_PRIMARY_KEY = "data";
49+
static final String SAVED_NOTIFICATION_FALLBACK_KEY = "fallback.data";
5050

5151
protected static int findResourceIdInContextByName(Context context, String name) {
5252
if (name == null)
@@ -108,7 +108,7 @@ protected static void serializeNotification(SharedPreferences prefs, Notificatio
108108
}
109109
}
110110

111-
private static boolean serializeNotificationParcel(Intent intent, DataOutputStream out) {
111+
static boolean serializeNotificationParcel(Intent intent, DataOutputStream out) {
112112
try {
113113
byte[] bytes = serializeParcelable(intent);
114114
if (bytes == null || bytes.length == 0)

com.unity.mobile.notifications/Tests/Runtime/Android/UnityNotificationTestUtils.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.unity.androidnotifications;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.DataOutputStream;
45
import android.app.Notification;
6+
import android.content.Intent;
7+
import android.content.SharedPreferences;
58
import android.os.Build;
9+
import android.util.Base64;
610
import android.util.Log;
711
import static com.unity.androidnotifications.UnityNotificationManager.KEY_ID;
812
import static com.unity.androidnotifications.UnityNotificationManager.KEY_FIRE_TIME;
@@ -11,8 +15,12 @@
1115
import static com.unity.androidnotifications.UnityNotificationManager.KEY_SHOW_IN_FOREGROUND;
1216
import static com.unity.androidnotifications.UnityNotificationManager.KEY_SMALL_ICON;
1317
import static com.unity.androidnotifications.UnityNotificationManager.KEY_LARGE_ICON;
18+
import static com.unity.androidnotifications.UnityNotificationManager.KEY_NOTIFICATION;
1419
import static com.unity.androidnotifications.UnityNotificationManager.TAG_UNITY;
1520
import static com.unity.androidnotifications.UnityNotificationUtilities.UNITY_MAGIC_NUMBER;
21+
import static com.unity.androidnotifications.UnityNotificationUtilities.SAVED_NOTIFICATION_PRIMARY_KEY;
22+
import static com.unity.androidnotifications.UnityNotificationUtilities.SAVED_NOTIFICATION_FALLBACK_KEY;
23+
import static com.unity.androidnotifications.UnityNotificationUtilities.serializeNotificationParcel;
1624
import static com.unity.androidnotifications.UnityNotificationUtilities.serializeParcelable;
1725
import static com.unity.androidnotifications.UnityNotificationUtilities.serializeString;
1826

@@ -114,4 +122,36 @@ private static boolean serializeNotificationCustom_v1(Notification notification,
114122
return false;
115123
}
116124
}
125+
126+
// copy-paste of serializeNotification when it was primary & fallback keys
127+
// minor altereration: call serializeNotificationCustom_v1
128+
private static void serializeNotification(SharedPreferences prefs, Notification notification) {
129+
try {
130+
String serialized = null, fallback = null;
131+
ByteArrayOutputStream data = new ByteArrayOutputStream();
132+
DataOutputStream out = new DataOutputStream(data);
133+
if (serializeNotificationCustom_v1(notification, out)) {
134+
out.flush();
135+
byte[] bytes = data.toByteArray();
136+
fallback = Base64.encodeToString(bytes, 0, bytes.length, 0);
137+
}
138+
data.reset();
139+
Intent intent = new Intent();
140+
intent.putExtra(KEY_NOTIFICATION, notification);
141+
if (serializeNotificationParcel(intent, out)) {
142+
out.close();
143+
byte[] bytes = data.toByteArray();
144+
serialized = Base64.encodeToString(bytes, 0, bytes.length, 0);
145+
}
146+
else
147+
serialized = fallback;
148+
149+
SharedPreferences.Editor editor = prefs.edit().clear();
150+
editor.putString(SAVED_NOTIFICATION_PRIMARY_KEY, serialized);
151+
editor.putString(SAVED_NOTIFICATION_FALLBACK_KEY, fallback);
152+
editor.apply();
153+
} catch (Exception e) {
154+
Log.e(TAG_UNITY, "Failed to serialize notification", e);
155+
}
156+
}
117157
}

0 commit comments

Comments
 (0)