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

9.09% Statements 3/33
5% Branches 1/20
40% Functions 2/5
9.09% Lines 3/33
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                                                                                                                                                                                       
/**
 * @flow
 * AndroidNotifications representation wrapper
 */
import { Platform } from 'react-native';
import AndroidChannel from './AndroidChannel';
import AndroidChannelGroup from './AndroidChannelGroup';
import { getNativeModule } from '../../utils/native';
 
import type Notifications from './';
 
export default class AndroidNotifications {
  _notifications: Notifications;
 
  constructor(notifications: Notifications) {
    this._notifications = notifications;
  }
 
  createChannel(channel: AndroidChannel): Promise<void> {
    Iif (Platform.OS === 'android') {
      if (!(channel instanceof AndroidChannel)) {
        throw new Error(
          `AndroidNotifications:createChannel expects an 'AndroidChannel' but got type ${typeof channel}`
        );
      }
      return getNativeModule(this._notifications).createChannel(
        channel.build()
      );
    }
    return Promise.resolve();
  }
 
  createChannelGroup(channelGroup: AndroidChannelGroup): Promise<void> {
    if (Platform.OS === 'android') {
      if (!(channelGroup instanceof AndroidChannelGroup)) {
        throw new Error(
          `AndroidNotifications:createChannelGroup expects an 'AndroidChannelGroup' but got type ${typeof channelGroup}`
        );
      }
      return getNativeModule(this._notifications).createChannelGroup(
        channelGroup.build()
      );
    }
    return Promise.resolve();
  }
 
  createChannelGroups(channelGroups: AndroidChannelGroup[]): Promise<void> {
    if (Platform.OS === 'android') {
      if (!Array.isArray(channelGroups)) {
        throw new Error(
          `AndroidNotifications:createChannelGroups expects an 'Array' but got type ${typeof channelGroups}`
        );
      }
      const nativeChannelGroups = [];
      for (let i = 0; i < channelGroups.length; i++) {
        const channelGroup = channelGroups[i];
        if (!(channelGroup instanceof AndroidChannelGroup)) {
          throw new Error(
            `AndroidNotifications:createChannelGroups expects array items of type 'AndroidChannelGroup' but got type ${typeof channelGroup}`
          );
        }
        nativeChannelGroups.push(channelGroup.build());
      }
      return getNativeModule(this._notifications).createChannelGroups(
        nativeChannelGroups
      );
    }
    return Promise.resolve();
  }
 
  createChannels(channels: AndroidChannel[]): Promise<void> {
    if (Platform.OS === 'android') {
      if (!Array.isArray(channels)) {
        throw new Error(
          `AndroidNotifications:createChannels expects an 'Array' but got type ${typeof channels}`
        );
      }
      const nativeChannels = [];
      for (let i = 0; i < channels.length; i++) {
        const channel = channels[i];
        if (!(channel instanceof AndroidChannel)) {
          throw new Error(
            `AndroidNotifications:createChannels expects array items of type 'AndroidChannel' but got type ${typeof channel}`
          );
        }
        nativeChannels.push(channel.build());
      }
      return getNativeModule(this._notifications).createChannels(
        nativeChannels
      );
    }
    return Promise.resolve();
  }
}