|
1 | 1 | package com.unity.androidnotifications;
|
2 | 2 |
|
| 3 | +import java.io.ByteArrayOutputStream; |
3 | 4 | import java.io.DataOutputStream;
|
4 | 5 | import android.app.Notification;
|
| 6 | +import android.content.Intent; |
| 7 | +import android.content.SharedPreferences; |
5 | 8 | import android.os.Build;
|
| 9 | +import android.util.Base64; |
6 | 10 | import android.util.Log;
|
7 | 11 | import static com.unity.androidnotifications.UnityNotificationManager.KEY_ID;
|
8 | 12 | import static com.unity.androidnotifications.UnityNotificationManager.KEY_FIRE_TIME;
|
|
11 | 15 | import static com.unity.androidnotifications.UnityNotificationManager.KEY_SHOW_IN_FOREGROUND;
|
12 | 16 | import static com.unity.androidnotifications.UnityNotificationManager.KEY_SMALL_ICON;
|
13 | 17 | import static com.unity.androidnotifications.UnityNotificationManager.KEY_LARGE_ICON;
|
| 18 | +import static com.unity.androidnotifications.UnityNotificationManager.KEY_NOTIFICATION; |
14 | 19 | import static com.unity.androidnotifications.UnityNotificationManager.TAG_UNITY;
|
15 | 20 | 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; |
16 | 24 | import static com.unity.androidnotifications.UnityNotificationUtilities.serializeParcelable;
|
17 | 25 | import static com.unity.androidnotifications.UnityNotificationUtilities.serializeString;
|
18 | 26 |
|
@@ -114,4 +122,36 @@ private static boolean serializeNotificationCustom_v1(Notification notification,
|
114 | 122 | return false;
|
115 | 123 | }
|
116 | 124 | }
|
| 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 | + } |
117 | 157 | }
|
0 commit comments