1 package expo.modules.notifications.notifications.model; 2 3 import android.net.Uri; 4 import android.os.Parcel; 5 import android.os.Parcelable; 6 7 import org.json.JSONException; 8 import org.json.JSONObject; 9 10 import java.io.IOException; 11 import java.io.ObjectStreamException; 12 import java.io.Serializable; 13 14 import androidx.annotation.Nullable; 15 16 import expo.modules.notifications.notifications.enums.NotificationPriority; 17 18 /** 19 * A POJO representing a notification content: title, message, body, etc. Instances 20 * should be created using {@link NotificationContent.Builder}. 21 */ 22 public class NotificationContent implements Parcelable, Serializable { 23 private String mTitle; 24 private String mText; 25 private String mSubtitle; 26 private Number mBadgeCount; 27 private boolean mShouldPlayDefaultSound; 28 private Uri mSound; 29 private boolean mShouldUseDefaultVibrationPattern; 30 private long[] mVibrationPattern; 31 private JSONObject mBody; 32 private NotificationPriority mPriority; 33 private Number mColor; 34 private boolean mAutoDismiss; 35 private String mCategoryId; 36 private boolean mSticky; 37 NotificationContent()38 protected NotificationContent() { 39 } 40 41 public static final Creator<NotificationContent> CREATOR = new Creator<NotificationContent>() { 42 @Override 43 public NotificationContent createFromParcel(Parcel in) { 44 return new NotificationContent(in); 45 } 46 47 @Override 48 public NotificationContent[] newArray(int size) { 49 return new NotificationContent[size]; 50 } 51 }; 52 53 @Nullable getTitle()54 public String getTitle() { 55 return mTitle; 56 } 57 58 @Nullable getText()59 public String getText() { 60 return mText; 61 } 62 63 @Nullable getSubtitle()64 public String getSubtitle() { 65 return mSubtitle; 66 } 67 68 @Nullable getBadgeCount()69 public Number getBadgeCount() { 70 return mBadgeCount; 71 } 72 shouldPlayDefaultSound()73 public boolean shouldPlayDefaultSound() { 74 return mShouldPlayDefaultSound; 75 } 76 77 @Nullable getSound()78 public Uri getSound() { 79 return mSound; 80 } 81 shouldUseDefaultVibrationPattern()82 public boolean shouldUseDefaultVibrationPattern() { 83 return mShouldUseDefaultVibrationPattern; 84 } 85 86 @Nullable getVibrationPattern()87 public long[] getVibrationPattern() { 88 return mVibrationPattern; 89 } 90 91 @Nullable getBody()92 public JSONObject getBody() { 93 return mBody; 94 } 95 96 @Nullable getPriority()97 public NotificationPriority getPriority() { 98 return mPriority; 99 } 100 101 @Nullable getColor()102 public Number getColor() { 103 return mColor; 104 } 105 isAutoDismiss()106 public boolean isAutoDismiss() { 107 return mAutoDismiss; 108 } 109 getCategoryId()110 public String getCategoryId() { 111 return mCategoryId; 112 } 113 isSticky()114 public boolean isSticky() { 115 return mSticky; 116 } 117 118 @Override describeContents()119 public int describeContents() { 120 return 0; 121 } 122 NotificationContent(Parcel in)123 protected NotificationContent(Parcel in) { 124 mTitle = in.readString(); 125 mText = in.readString(); 126 mSubtitle = in.readString(); 127 mBadgeCount = (Number) in.readSerializable(); 128 mShouldPlayDefaultSound = in.readByte() != 0; 129 mSound = in.readParcelable(getClass().getClassLoader()); 130 mShouldUseDefaultVibrationPattern = in.readByte() != 0; 131 mVibrationPattern = in.createLongArray(); 132 try { 133 mBody = new JSONObject(in.readString()); 134 } catch (JSONException | NullPointerException e) { 135 // do nothing 136 } 137 Number priorityNumber = (Number) in.readSerializable(); 138 if (priorityNumber != null) { 139 mPriority = NotificationPriority.fromNativeValue(priorityNumber.intValue()); 140 } 141 mColor = (Number) in.readSerializable(); 142 mAutoDismiss = in.readByte() == 1; 143 mCategoryId = in.readString(); 144 mSticky = in.readByte() == 1; 145 } 146 147 @Override writeToParcel(Parcel dest, int flags)148 public void writeToParcel(Parcel dest, int flags) { 149 dest.writeString(mTitle); 150 dest.writeString(mText); 151 dest.writeString(mSubtitle); 152 dest.writeSerializable(mBadgeCount); 153 dest.writeByte((byte) (mShouldPlayDefaultSound ? 1 : 0)); 154 dest.writeParcelable(mSound, 0); 155 dest.writeByte((byte) (mShouldUseDefaultVibrationPattern ? 1 : 0)); 156 dest.writeLongArray(mVibrationPattern); 157 dest.writeString(mBody != null ? mBody.toString() : null); 158 dest.writeSerializable(mPriority != null ? mPriority.getNativeValue() : null); 159 dest.writeSerializable(mColor); 160 dest.writeByte((byte) (mAutoDismiss ? 1 : 0)); 161 dest.writeString(mCategoryId); 162 dest.writeByte((byte) (mSticky ? 1 : 0)); 163 } 164 165 // EXPONOTIFCONTENT02 166 private static final long serialVersionUID = 397666843266836802L; 167 writeObject(java.io.ObjectOutputStream out)168 private void writeObject(java.io.ObjectOutputStream out) throws IOException { 169 out.writeObject(mTitle); 170 out.writeObject(mText); 171 out.writeObject(mSubtitle); 172 out.writeObject(mBadgeCount); 173 out.writeByte(mShouldPlayDefaultSound ? 1 : 0); 174 out.writeObject(mSound == null ? null : mSound.toString()); 175 out.writeByte(mShouldUseDefaultVibrationPattern ? 1 : 0); 176 if (mVibrationPattern == null) { 177 out.writeInt(-1); 178 } else { 179 out.writeInt(mVibrationPattern.length); 180 for (long duration : mVibrationPattern) { 181 out.writeLong(duration); 182 } 183 } 184 out.writeObject(mBody != null ? mBody.toString() : null); 185 out.writeObject(mPriority != null ? mPriority.getNativeValue() : null); 186 out.writeObject(mColor); 187 out.writeByte(mAutoDismiss ? 1 : 0); 188 out.writeObject(mCategoryId != null ? mCategoryId.toString() : null); 189 out.writeByte(mSticky ? 1 : 0); 190 } 191 readObject(java.io.ObjectInputStream in)192 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { 193 mTitle = (String) in.readObject(); 194 mText = (String) in.readObject(); 195 mSubtitle = (String) in.readObject(); 196 mBadgeCount = (Number) in.readObject(); 197 mShouldPlayDefaultSound = in.readByte() == 1; 198 String soundUri = (String) in.readObject(); 199 if (soundUri == null) { 200 mSound = null; 201 } else { 202 mSound = Uri.parse(soundUri); 203 } 204 mShouldUseDefaultVibrationPattern = in.readByte() == 1; 205 int vibrationPatternLength = in.readInt(); 206 if (vibrationPatternLength < 0) { 207 mVibrationPattern = null; 208 } else { 209 mVibrationPattern = new long[vibrationPatternLength]; 210 for (int i = 0; i < vibrationPatternLength; i++) { 211 mVibrationPattern[i] = in.readLong(); 212 } 213 } 214 String bodyString = (String) in.readObject(); 215 if (bodyString == null) { 216 mBody = null; 217 } else { 218 try { 219 mBody = new JSONObject(bodyString); 220 } catch (JSONException | NullPointerException e) { 221 // do nothing 222 } 223 } 224 Number priorityNumber = (Number) in.readObject(); 225 if (priorityNumber != null) { 226 mPriority = NotificationPriority.fromNativeValue(priorityNumber.intValue()); 227 } 228 mColor = (Number) in.readObject(); 229 mAutoDismiss = in.readByte() == 1; 230 String categoryIdString = (String) in.readObject(); 231 if (categoryIdString == null) { 232 mCategoryId = null; 233 } else { 234 mCategoryId = new String(categoryIdString); 235 } 236 mSticky = in.readByte() == 1; 237 } 238 readObjectNoData()239 private void readObjectNoData() throws ObjectStreamException { 240 } 241 242 public static class Builder { 243 private String mTitle; 244 private String mText; 245 private String mSubtitle; 246 private Number mBadgeCount; 247 private boolean mShouldPlayDefaultSound; 248 private Uri mSound; 249 private boolean mShouldUseDefaultVibrationPattern; 250 private long[] mVibrationPattern; 251 private JSONObject mBody; 252 private NotificationPriority mPriority; 253 private Number mColor; 254 private boolean mAutoDismiss; 255 private String mCategoryId; 256 private boolean mSticky; 257 Builder()258 public Builder() { 259 useDefaultSound(); 260 useDefaultVibrationPattern(); 261 } 262 setTitle(String title)263 public Builder setTitle(String title) { 264 mTitle = title; 265 return this; 266 } 267 setSubtitle(String subtitle)268 public Builder setSubtitle(String subtitle) { 269 mSubtitle = subtitle; 270 return this; 271 } 272 setText(String text)273 public Builder setText(String text) { 274 mText = text; 275 return this; 276 } 277 setBody(JSONObject body)278 public Builder setBody(JSONObject body) { 279 mBody = body; 280 return this; 281 } 282 setPriority(NotificationPriority priority)283 public Builder setPriority(NotificationPriority priority) { 284 mPriority = priority; 285 return this; 286 } 287 setBadgeCount(Number badgeCount)288 public Builder setBadgeCount(Number badgeCount) { 289 mBadgeCount = badgeCount; 290 return this; 291 } 292 useDefaultVibrationPattern()293 public Builder useDefaultVibrationPattern() { 294 mShouldUseDefaultVibrationPattern = true; 295 mVibrationPattern = null; 296 return this; 297 } 298 setVibrationPattern(long[] vibrationPattern)299 public Builder setVibrationPattern(long[] vibrationPattern) { 300 mShouldUseDefaultVibrationPattern = false; 301 mVibrationPattern = vibrationPattern; 302 return this; 303 } 304 useDefaultSound()305 public Builder useDefaultSound() { 306 mShouldPlayDefaultSound = true; 307 mSound = null; 308 return this; 309 } 310 setSound(Uri sound)311 public Builder setSound(Uri sound) { 312 mShouldPlayDefaultSound = false; 313 mSound = sound; 314 return this; 315 } 316 setColor(Number color)317 public Builder setColor(Number color) { 318 mColor = color; 319 return this; 320 } 321 setAutoDismiss(boolean autoDismiss)322 public Builder setAutoDismiss(boolean autoDismiss) { 323 mAutoDismiss = autoDismiss; 324 return this; 325 } 326 setCategoryId(String categoryId)327 public Builder setCategoryId(String categoryId) { 328 mCategoryId = categoryId; 329 return this; 330 } 331 setSticky(boolean sticky)332 public Builder setSticky(boolean sticky) { 333 mSticky = sticky; 334 return this; 335 } 336 build()337 public NotificationContent build() { 338 NotificationContent content = new NotificationContent(); 339 content.mTitle = mTitle; 340 content.mSubtitle = mSubtitle; 341 content.mText = mText; 342 content.mBadgeCount = mBadgeCount; 343 content.mShouldUseDefaultVibrationPattern = mShouldUseDefaultVibrationPattern; 344 content.mVibrationPattern = mVibrationPattern; 345 content.mShouldPlayDefaultSound = mShouldPlayDefaultSound; 346 content.mSound = mSound; 347 content.mBody = mBody; 348 content.mPriority = mPriority; 349 content.mColor = mColor; 350 content.mAutoDismiss = mAutoDismiss; 351 content.mCategoryId = mCategoryId; 352 content.mSticky = mSticky; 353 return content; 354 } 355 } 356 } 357