all files / firebase/modules/notifications/ AndroidNotification.js

9.87% Statements 15/152
37.5% Branches 9/24
7.25% Functions 5/69
9.87% Lines 15/152
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
/**
 * @flow
 * AndroidNotification representation wrapper
 */
import AndroidAction, { fromNativeAndroidAction } from './AndroidAction';
import { BadgeIconType, Category, GroupAlert, Priority } from './types';
import type Notification from './Notification';
import type {
  BadgeIconTypeType,
  CategoryType,
  DefaultsType,
  GroupAlertType,
  Lights,
  NativeAndroidNotification,
  PriorityType,
  Progress,
  SmallIcon,
  VisibilityType,
} from './types';
 
export default class AndroidNotification {
  _actions: AndroidAction[];
  _autoCancel: boolean | void;
  _badgeIconType: BadgeIconTypeType | void;
  _category: CategoryType | void;
  _channelId: string;
  _clickAction: string | void;
  _color: string | void;
  _colorized: boolean | void;
  _contentInfo: string | void;
  _defaults: DefaultsType[] | void;
  _group: string | void;
  _groupAlertBehaviour: GroupAlertType | void;
  _groupSummary: boolean | void;
  _largeIcon: string | void;
  _lights: Lights | void;
  _localOnly: boolean | void;
  _notification: Notification;
  _number: number | void;
  _ongoing: boolean | void;
  _onlyAlertOnce: boolean | void;
  _people: string[];
  _priority: PriorityType | void;
  _progress: Progress | void;
  // _publicVersion: Notification;
  _remoteInputHistory: string[] | void;
  _shortcutId: string | void;
  _showWhen: boolean | void;
  _smallIcon: SmallIcon;
  _sortKey: string | void;
  // TODO: style: Style; // Need to figure out if this can work
  _ticker: string | void;
  _timeoutAfter: number | void;
  _usesChronometer: boolean | void;
  _vibrate: number[] | void;
  _visibility: VisibilityType | void;
  _when: number | void;
 
  // android unsupported
  // content: RemoteViews
  // contentIntent: PendingIntent - need to look at what this is
  // customBigContentView: RemoteViews
  // customContentView: RemoteViews
  // customHeadsUpContentView: RemoteViews
  // deleteIntent: PendingIntent
  // fullScreenIntent: PendingIntent
  // sound.streamType
 
  constructor(notification: Notification, data?: NativeAndroidNotification) {
    this._notification = notification;
 
    Iif (data) {
      this._actions = data.actions
        ? data.actions.map(action => fromNativeAndroidAction(action))
        : [];
      this._autoCancel = data.autoCancel;
      this._badgeIconType = data.badgeIconType;
      this._category = data.category;
      this._channelId = data.channelId;
      this._clickAction = data.clickAction;
      this._color = data.color;
      this._colorized = data.colorized;
      this._contentInfo = data.contentInfo;
      this._defaults = data.defaults;
      this._group = data.group;
      this._groupAlertBehaviour = data.groupAlertBehaviour;
      this._groupSummary = data.groupSummary;
      this._largeIcon = data.largeIcon;
      this._lights = data.lights;
      this._localOnly = data.localOnly;
      this._number = data.number;
      this._ongoing = data.ongoing;
      this._onlyAlertOnce = data.onlyAlertOnce;
      this._people = data.people;
      this._priority = data.priority;
      this._progress = data.progress;
      // _publicVersion: Notification;
      this._remoteInputHistory = data.remoteInputHistory;
      this._shortcutId = data.shortcutId;
      this._showWhen = data.showWhen;
      this._smallIcon = data.smallIcon;
      this._sortKey = data.sortKey;
      this._ticker = data.ticker;
      this._timeoutAfter = data.timeoutAfter;
      this._usesChronometer = data.usesChronometer;
      this._vibrate = data.vibrate;
      this._visibility = data.visibility;
      this._when = data.when;
    }
 
    // Defaults
    this._actions = this._actions || [];
    this._people = this._people || [];
    this._smallIcon = this._smallIcon || {
      icon: 'ic_launcher',
    };
  }
 
  get actions(): AndroidAction[] {
    return this._actions;
  }
 
  get autoCancel(): ?boolean {
    return this._autoCancel;
  }
 
  get badgeIconType(): ?BadgeIconTypeType {
    return this._badgeIconType;
  }
 
  get category(): ?CategoryType {
    return this._category;
  }
 
  get channelId(): string {
    return this._channelId;
  }
 
  get clickAction(): ?string {
    return this._clickAction;
  }
 
  get color(): ?string {
    return this._color;
  }
 
  get colorized(): ?boolean {
    return this._colorized;
  }
 
  get contentInfo(): ?string {
    return this._contentInfo;
  }
 
  get defaults(): ?(DefaultsType[]) {
    return this._defaults;
  }
 
  get group(): ?string {
    return this._group;
  }
 
  get groupAlertBehaviour(): ?GroupAlertType {
    return this._groupAlertBehaviour;
  }
 
  get groupSummary(): ?boolean {
    return this._groupSummary;
  }
 
  get largeIcon(): ?string {
    return this._largeIcon;
  }
 
  get lights(): ?Lights {
    return this._lights;
  }
 
  get localOnly(): ?boolean {
    return this._localOnly;
  }
 
  get number(): ?number {
    return this._number;
  }
 
  get ongoing(): ?boolean {
    return this._ongoing;
  }
 
  get onlyAlertOnce(): ?boolean {
    return this._onlyAlertOnce;
  }
 
  get people(): string[] {
    return this._people;
  }
 
  get priority(): ?PriorityType {
    return this._priority;
  }
 
  get progress(): ?Progress {
    return this._progress;
  }
 
  get remoteInputHistory(): ?(string[]) {
    return this._remoteInputHistory;
  }
 
  get shortcutId(): ?string {
    return this._shortcutId;
  }
 
  get showWhen(): ?boolean {
    return this._showWhen;
  }
 
  get smallIcon(): SmallIcon {
    return this._smallIcon;
  }
 
  get sortKey(): ?string {
    return this._sortKey;
  }
 
  get ticker(): ?string {
    return this._ticker;
  }
 
  get timeoutAfter(): ?number {
    return this._timeoutAfter;
  }
 
  get usesChronometer(): ?boolean {
    return this._usesChronometer;
  }
 
  get vibrate(): ?(number[]) {
    return this._vibrate;
  }
 
  get visibility(): ?VisibilityType {
    return this._visibility;
  }
 
  get when(): ?number {
    return this._when;
  }
 
  /**
   *
   * @param action
   * @returns {Notification}
   */
  addAction(action: AndroidAction): Notification {
    Iif (!(action instanceof AndroidAction)) {
      throw new Error(
        `AndroidNotification:addAction expects an 'AndroidAction' but got type ${typeof action}`
      );
    }
    this._actions.push(action);
    return this._notification;
  }
 
  /**
   *
   * @param person
   * @returns {Notification}
   */
  addPerson(person: string): Notification {
    this._people.push(person);
    return this._notification;
  }
 
  /**
   *
   * @param autoCancel
   * @returns {Notification}
   */
  setAutoCancel(autoCancel: boolean): Notification {
    this._autoCancel = autoCancel;
    return this._notification;
  }
 
  /**
   *
   * @param badgeIconType
   * @returns {Notification}
   */
  setBadgeIconType(badgeIconType: BadgeIconTypeType): Notification {
    if (!Object.values(BadgeIconType).includes(badgeIconType)) {
      throw new Error(
        `AndroidNotification:setBadgeIconType Invalid BadgeIconType: ${badgeIconType}`
      );
    }
    this._badgeIconType = badgeIconType;
    return this._notification;
  }
 
  /**
   *
   * @param category
   * @returns {Notification}
   */
  setCategory(category: CategoryType): Notification {
    if (!Object.values(Category).includes(category)) {
      throw new Error(
        `AndroidNotification:setCategory Invalid Category: ${category}`
      );
    }
    this._category = category;
    return this._notification;
  }
 
  /**
   *
   * @param channelId
   * @returns {Notification}
   */
  setChannelId(channelId: string): Notification {
    this._channelId = channelId;
    return this._notification;
  }
 
  /**
   *
   * @param clickAction
   * @returns {Notification}
   */
  setClickAction(clickAction: string): Notification {
    this._clickAction = clickAction;
    return this._notification;
  }
 
  /**
   *
   * @param color
   * @returns {Notification}
   */
  setColor(color: string): Notification {
    this._color = color;
    return this._notification;
  }
 
  /**
   *
   * @param colorized
   * @returns {Notification}
   */
  setColorized(colorized: boolean): Notification {
    this._colorized = colorized;
    return this._notification;
  }
 
  /**
   *
   * @param contentInfo
   * @returns {Notification}
   */
  setContentInfo(contentInfo: string): Notification {
    this._contentInfo = contentInfo;
    return this._notification;
  }
 
  /**
   *
   * @param defaults
   * @returns {Notification}
   */
  setDefaults(defaults: DefaultsType[]): Notification {
    this._defaults = defaults;
    return this._notification;
  }
 
  /**
   *
   * @param group
   * @returns {Notification}
   */
  setGroup(group: string): Notification {
    this._group = group;
    return this._notification;
  }
 
  /**
   *
   * @param groupAlertBehaviour
   * @returns {Notification}
   */
  setGroupAlertBehaviour(groupAlertBehaviour: GroupAlertType): Notification {
    if (!Object.values(GroupAlert).includes(groupAlertBehaviour)) {
      throw new Error(
        `AndroidNotification:setGroupAlertBehaviour Invalid GroupAlert: ${groupAlertBehaviour}`
      );
    }
    this._groupAlertBehaviour = groupAlertBehaviour;
    return this._notification;
  }
 
  /**
   *
   * @param groupSummary
   * @returns {Notification}
   */
  setGroupSummary(groupSummary: boolean): Notification {
    this._groupSummary = groupSummary;
    return this._notification;
  }
 
  /**
   *
   * @param largeIcon
   * @returns {Notification}
   */
  setLargeIcon(largeIcon: string): Notification {
    this._largeIcon = largeIcon;
    return this._notification;
  }
 
  /**
   *
   * @param argb
   * @param onMs
   * @param offMs
   * @returns {Notification}
   */
  setLights(argb: number, onMs: number, offMs: number): Notification {
    this._lights = {
      argb,
      onMs,
      offMs,
    };
    return this._notification;
  }
 
  /**
   *
   * @param localOnly
   * @returns {Notification}
   */
  setLocalOnly(localOnly: boolean): Notification {
    this._localOnly = localOnly;
    return this._notification;
  }
 
  /**
   *
   * @param number
   * @returns {Notification}
   */
  setNumber(number: number): Notification {
    this._number = number;
    return this._notification;
  }
 
  /**
   *
   * @param ongoing
   * @returns {Notification}
   */
  setOngoing(ongoing: boolean): Notification {
    this._ongoing = ongoing;
    return this._notification;
  }
 
  /**
   *
   * @param onlyAlertOnce
   * @returns {Notification}
   */
  setOnlyAlertOnce(onlyAlertOnce: boolean): Notification {
    this._onlyAlertOnce = onlyAlertOnce;
    return this._notification;
  }
 
  /**
   *
   * @param priority
   * @returns {Notification}
   */
  setPriority(priority: PriorityType): Notification {
    Iif (!Object.values(Priority).includes(priority)) {
      throw new Error(
        `AndroidNotification:setPriority Invalid Priority: ${priority}`
      );
    }
    this._priority = priority;
    return this._notification;
  }
 
  /**
   *
   * @param max
   * @param progress
   * @param indeterminate
   * @returns {Notification}
   */
  setProgress(
    max: number,
    progress: number,
    indeterminate: boolean
  ): Notification {
    this._progress = {
      max,
      progress,
      indeterminate,
    };
    return this._notification;
  }
 
  /**
   *
   * @param publicVersion
   * @returns {Notification}
   */
  /* setPublicVersion(publicVersion: Notification): Notification {
    this._publicVersion = publicVersion;
    return this._notification;
  } */
 
  /**
   *
   * @param remoteInputHistory
   * @returns {Notification}
   */
  setRemoteInputHistory(remoteInputHistory: string[]): Notification {
    this._remoteInputHistory = remoteInputHistory;
    return this._notification;
  }
 
  /**
   *
   * @param shortcutId
   * @returns {Notification}
   */
  setShortcutId(shortcutId: string): Notification {
    this._shortcutId = shortcutId;
    return this._notification;
  }
 
  /**
   *
   * @param showWhen
   * @returns {Notification}
   */
  setShowWhen(showWhen: boolean): Notification {
    this._showWhen = showWhen;
    return this._notification;
  }
 
  /**
   *
   * @param icon
   * @param level
   * @returns {Notification}
   */
  setSmallIcon(icon: string, level?: number): Notification {
    this._smallIcon = {
      icon,
      level,
    };
    return this._notification;
  }
 
  /**
   *
   * @param sortKey
   * @returns {Notification}
   */
  setSortKey(sortKey: string): Notification {
    this._sortKey = sortKey;
    return this._notification;
  }
 
  /**
   *
   * @param ticker
   * @returns {Notification}
   */
  setTicker(ticker: string): Notification {
    this._ticker = ticker;
    return this._notification;
  }
 
  /**
   *
   * @param timeoutAfter
   * @returns {Notification}
   */
  setTimeoutAfter(timeoutAfter: number): Notification {
    this._timeoutAfter = timeoutAfter;
    return this._notification;
  }
 
  /**
   *
   * @param usesChronometer
   * @returns {Notification}
   */
  setUsesChronometer(usesChronometer: boolean): Notification {
    this._usesChronometer = usesChronometer;
    return this._notification;
  }
 
  /**
   *
   * @param vibrate
   * @returns {Notification}
   */
  setVibrate(vibrate: number[]): Notification {
    this._vibrate = vibrate;
    return this._notification;
  }
 
  /**
   *
   * @param when
   * @returns {Notification}
   */
  setWhen(when: number): Notification {
    this._when = when;
    return this._notification;
  }
 
  build(): NativeAndroidNotification {
    // TODO: Validation of required fields
    if (!this._channelId) {
      throw new Error(
        'AndroidNotification: Missing required `channelId` property'
      );
    } else if (!this._smallIcon) {
      throw new Error(
        'AndroidNotification: Missing required `smallIcon` property'
      );
    }
 
    return {
      actions: this._actions.map(action => action.build()),
      autoCancel: this._autoCancel,
      badgeIconType: this._badgeIconType,
      category: this._category,
      channelId: this._channelId,
      clickAction: this._clickAction,
      color: this._color,
      colorized: this._colorized,
      contentInfo: this._contentInfo,
      defaults: this._defaults,
      group: this._group,
      groupAlertBehaviour: this._groupAlertBehaviour,
      groupSummary: this._groupSummary,
      largeIcon: this._largeIcon,
      lights: this._lights,
      localOnly: this._localOnly,
      number: this._number,
      ongoing: this._ongoing,
      onlyAlertOnce: this._onlyAlertOnce,
      people: this._people,
      priority: this._priority,
      progress: this._progress,
      // publicVersion: this._publicVersion,
      remoteInputHistory: this._remoteInputHistory,
      shortcutId: this._shortcutId,
      showWhen: this._showWhen,
      smallIcon: this._smallIcon,
      sortKey: this._sortKey,
      // TODO: style: Style,
      ticker: this._ticker,
      timeoutAfter: this._timeoutAfter,
      usesChronometer: this._usesChronometer,
      vibrate: this._vibrate,
      visibility: this._visibility,
      when: this._when,
    };
  }
}