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

55% Statements 22/40
65.22% Branches 15/23
37.5% Functions 6/16
55% Lines 22/40
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                                                                                                                                                                                                                                                                                                       
/**
 * @flow
 * Notification representation wrapper
 */
import { Platform } from 'react-native';
import AndroidNotification from './AndroidNotification';
import IOSNotification from './IOSNotification';
import { generatePushID, isObject } from '../../utils';
 
import type { NativeNotification } from './types';
 
export type NotificationOpen = {|
  action: string,
  notification: Notification,
  results?: { [string]: string },
|};
 
export default class Notification {
  // iOS 8/9 | 10+ | Android
  _android: AndroidNotification;
  _body: string; // alertBody | body | contentText
  _data: { [string]: string }; // userInfo | userInfo | extras
  _ios: IOSNotification;
  _notificationId: string;
  _sound: string | void; // soundName | sound | sound
  _subtitle: string | void; // N/A | subtitle | subText
  _title: string; // alertTitle | title | contentTitle
 
  constructor(data?: NativeNotification) {
    this._android = new AndroidNotification(this, data && data.android);
    this._ios = new IOSNotification(this, data && data.ios);
 
    if (data) {
      this._body = data.body;
      this._data = data.data;
      this._notificationId = data.notificationId;
      this._sound = data.sound;
      this._subtitle = data.subtitle;
      this._title = data.title;
    }
 
    // Defaults
    this._data = this._data || {};
    // TODO: Is this the best way to generate an ID?
    this._notificationId = this._notificationId || generatePushID();
  }
 
  get android(): AndroidNotification {
    return this._android;
  }
 
  get body(): string {
    return this._body;
  }
 
  get data(): { [string]: string } {
    return this._data;
  }
 
  get ios(): IOSNotification {
    return this._ios;
  }
 
  get notificationId(): string {
    return this._notificationId;
  }
 
  get sound(): ?string {
    return this._sound;
  }
 
  get subtitle(): ?string {
    return this._subtitle;
  }
 
  get title(): string {
    return this._title;
  }
 
  /**
   *
   * @param body
   * @returns {Notification}
   */
  setBody(body: string): Notification {
    this._body = body;
    return this;
  }
 
  /**
   *
   * @param data
   * @returns {Notification}
   */
  setData(data: Object = {}): Notification {
    if (!isObject(data)) {
      throw new Error(
        `Notification:withData expects an object but got type '${typeof data}'.`
      );
    }
    this._data = data;
    return this;
  }
 
  /**
   *
   * @param notificationId
   * @returns {Notification}
   */
  setNotificationId(notificationId: string): Notification {
    this._notificationId = notificationId;
    return this;
  }
 
  /**
   *
   * @param sound
   * @returns {Notification}
   */
  setSound(sound: string): Notification {
    this._sound = sound;
    return this;
  }
 
  /**
   *
   * @param subtitle
   * @returns {Notification}
   */
  setSubtitle(subtitle: string): Notification {
    this._subtitle = subtitle;
    return this;
  }
 
  /**
   *
   * @param title
   * @returns {Notification}
   */
  setTitle(title: string): Notification {
    this._title = title;
    return this;
  }
 
  build(): NativeNotification {
    // Android required fields: body, title, smallicon
    // iOS required fields: TODO
    Iif (!this._body) {
      throw new Error('Notification: Missing required `body` property');
    } else Iif (!this._notificationId) {
      throw new Error(
        'Notification: Missing required `notificationId` property'
      );
    } else Iif (!this._title) {
      throw new Error('Notification: Missing required `title` property');
    }
 
    return {
      android: Platform.OS === 'android' ? this._android.build() : undefined,
      body: this._body,
      data: this._data,
      ios: Platform.OS === 'ios' ? this._ios.build() : undefined,
      notificationId: this._notificationId,
      sound: this._sound,
      subtitle: this._subtitle,
      title: this._title,
    };
  }
}