@@ -72,39 +72,37 @@ protected static int findResourceIdInContextByName(Context context, String name)
72
72
Unfortunately, while Notification itself is Parcelable and can be marshalled to bytes,
73
73
it's contents are not guaranteed to be (Binder objects).
74
74
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
80
77
*/
81
78
protected static void serializeNotification (SharedPreferences prefs , Notification notification , boolean serializeParcel ) {
82
79
try {
83
- String serialized = null , fallback = null ;
80
+ String serialized ;
84
81
ByteArrayOutputStream data = new ByteArrayOutputStream ();
85
82
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
+ }
98
93
}
99
94
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
+ }
102
102
}
103
103
104
104
SharedPreferences .Editor editor = prefs .edit ().clear ();
105
105
editor .putString (SAVED_NOTIFICATION_PRIMARY_KEY , serialized );
106
- if (fallback != null )
107
- editor .putString (SAVED_NOTIFICATION_FALLBACK_KEY , fallback );
108
106
editor .apply ();
109
107
} catch (Exception e ) {
110
108
Log .e (TAG_UNITY , "Failed to serialize notification" , e );
0 commit comments