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

34.38% Statements 11/32
100% Branches 4/4
12.5% Functions 2/16
34.38% Lines 11/32
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                                                                                                                                                                                                                                                                                                           
/**
 * @flow
 * IOSNotification representation wrapper
 */
import type Notification from './Notification';
import type {
  IOSAttachment,
  IOSAttachmentOptions,
  NativeIOSNotification,
} from './types';
 
export default class IOSNotification {
  _alertAction: string | void; // alertAction | N/A
  _attachments: IOSAttachment[]; // N/A | attachments
  _badge: number | void; // applicationIconBadgeNumber | badge
  _category: string | void;
  _hasAction: boolean | void; // hasAction | N/A
  _launchImage: string | void; // alertLaunchImage | launchImageName
  _notification: Notification;
  _threadIdentifier: string | void; // N/A | threadIdentifier
 
  constructor(notification: Notification, data?: NativeIOSNotification) {
    this._notification = notification;
 
    if (data) {
      this._alertAction = data.alertAction;
      this._attachments = data.attachments;
      this._badge = data.badge;
      this._category = data.category;
      this._hasAction = data.hasAction;
      this._launchImage = data.launchImage;
      this._threadIdentifier = data.threadIdentifier;
    }
 
    // Defaults
    this._attachments = this._attachments || [];
  }
 
  get alertAction(): ?string {
    return this._alertAction;
  }
 
  get attachments(): IOSAttachment[] {
    return this._attachments;
  }
 
  get badge(): ?number {
    return this._badge;
  }
 
  get category(): ?string {
    return this._category;
  }
 
  get hasAction(): ?boolean {
    return this._hasAction;
  }
 
  get launchImage(): ?string {
    return this._launchImage;
  }
 
  get threadIdentifier(): ?string {
    return this._threadIdentifier;
  }
 
  /**
   *
   * @param identifier
   * @param url
   * @param options
   * @returns {Notification}
   */
  addAttachment(
    identifier: string,
    url: string,
    options?: IOSAttachmentOptions
  ): Notification {
    this._attachments.push({
      identifier,
      options,
      url,
    });
    return this._notification;
  }
 
  /**
   *
   * @param alertAction
   * @returns {Notification}
   */
  setAlertAction(alertAction: string): Notification {
    this._alertAction = alertAction;
    return this._notification;
  }
 
  /**
   *
   * @param badge
   * @returns {Notification}
   */
  setBadge(badge: number): Notification {
    this._badge = badge;
    return this._notification;
  }
 
  /**
   *
   * @param category
   * @returns {Notification}
   */
  setCategory(category: string): Notification {
    this._category = category;
    return this._notification;
  }
 
  /**
   *
   * @param hasAction
   * @returns {Notification}
   */
  setHasAction(hasAction: boolean): Notification {
    this._hasAction = hasAction;
    return this._notification;
  }
 
  /**
   *
   * @param launchImage
   * @returns {Notification}
   */
  setLaunchImage(launchImage: string): Notification {
    this._launchImage = launchImage;
    return this._notification;
  }
 
  /**
   *
   * @param threadIdentifier
   * @returns {Notification}
   */
  setThreadIdentifier(threadIdentifier: string): Notification {
    this._threadIdentifier = threadIdentifier;
    return this._notification;
  }
 
  build(): NativeIOSNotification {
    // TODO: Validation of required fields
 
    return {
      alertAction: this._alertAction,
      attachments: this._attachments,
      badge: this._badge,
      category: this._category,
      hasAction: this._hasAction,
      launchImage: this._launchImage,
      threadIdentifier: this._threadIdentifier,
    };
  }
}