Skip to content

Commit 365eb4b

Browse files
committed
Ditch fallback
1 parent 0aa8bfd commit 365eb4b

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,39 +72,37 @@ protected static int findResourceIdInContextByName(Context context, String name)
7272
Unfortunately, while Notification itself is Parcelable and can be marshalled to bytes,
7373
it's contents are not guaranteed to be (Binder objects).
7474
Hence what we try to do here is:
75-
- serialize as is
76-
- fallback 1: serialize our known properties + serialize extras as is
77-
- fallback 2: serialize our known stuff
78-
When notification is serialized as-is, it may contain references to resources and in case
79-
of app update may fail to deserialize due to resources now missing, hence always save fallback version.
75+
- serialize as is if notification is possibly customized by user
76+
- otherwise serialize our stuff, since there is nothing more
8077
*/
8178
protected static void serializeNotification(SharedPreferences prefs, Notification notification, boolean serializeParcel) {
8279
try {
83-
String serialized = null, fallback = null;
80+
String serialized;
8481
ByteArrayOutputStream data = new ByteArrayOutputStream();
8582
DataOutputStream out = new DataOutputStream(data);
86-
if (serializeNotificationCustom(notification, out)) {
87-
out.flush();
88-
byte[] bytes = data.toByteArray();
89-
fallback = Base64.encodeToString(bytes, 0, bytes.length, 0);
90-
}
91-
data.reset();
92-
Intent intent = new Intent();
93-
intent.putExtra(KEY_NOTIFICATION, notification);
94-
if (serializeNotificationParcel(intent, out)) {
95-
out.close();
96-
byte[] bytes = data.toByteArray();
97-
serialized = Base64.encodeToString(bytes, 0, bytes.length, 0);
83+
if (serializeParcel) {
84+
Intent intent = new Intent();
85+
intent.putExtra(KEY_NOTIFICATION, notification);
86+
if (serializeNotificationParcel(intent, out)) {
87+
out.close();
88+
byte[] bytes = data.toByteArray();
89+
serialized = Base64.encodeToString(bytes, 0, bytes.length, 0);
90+
} else {
91+
return; // failed
92+
}
9893
}
9994
else {
100-
serialized = fallback;
101-
fallback = null;
95+
if (serializeNotificationCustom(notification, out)) {
96+
out.flush();
97+
byte[] bytes = data.toByteArray();
98+
serialized = Base64.encodeToString(bytes, 0, bytes.length, 0);
99+
} else {
100+
return; // failed
101+
}
102102
}
103103

104104
SharedPreferences.Editor editor = prefs.edit().clear();
105105
editor.putString(SAVED_NOTIFICATION_PRIMARY_KEY, serialized);
106-
if (fallback != null)
107-
editor.putString(SAVED_NOTIFICATION_FALLBACK_KEY, fallback);
108106
editor.apply();
109107
} catch (Exception e) {
110108
Log.e(TAG_UNITY, "Failed to serialize notification", e);

0 commit comments

Comments
 (0)