1import { Notifications } from 'expo';
2
3import Constants from 'expo-constants';
4
5const demoBodies: { [type: string]: any } = {
6  simple: {
7    title: 'Welcome to Expo!',
8    body: 'Native Component List is registered for push notifications.',
9    data: { example: 'sample data' },
10  },
11  image: {
12    title: 'Kodiak bear',
13    body:
14      'A Kodiak bear in Kodiak National Wildlife Refuge, Alaska, United States.\n\nSource: https://commons.wikimedia.org/wiki/File:2010-kodiak-bear-1.jpg',
15    richContent: {
16      image: 'https://upload.wikimedia.org/wikipedia/commons/7/71/2010-kodiak-bear-1.jpg',
17    },
18    data: {
19      trinomialName: 'Ursus arctos middendorffi',
20    },
21  },
22  audio: {
23    title: 'Moonlight',
24    body:
25      'Piano Sonata No. 14 in C sharp minor "Moonlight". Recorded 1924.\n\nSource: https://www.gutenberg.org/ebooks/10178',
26    richContent: {
27      audio: 'https://www.gutenberg.org/files/10178/10178-m/10178-m-001.mp3',
28    },
29    data: {
30      composer: 'Ludwig van Beethoven',
31    },
32  },
33  gif: {
34    title: 'Phenakistoscope',
35    body:
36      "Eadweard Muybridge's Phenakistoscope: A Couple Waltzing.\n\nSource: https://commons.wikimedia.org/wiki/File:Phenakistoscope_3g07690d.gif",
37    richContent: {
38      video: 'https://upload.wikimedia.org/wikipedia/commons/d/d3/Phenakistoscope_3g07690d.gif',
39    },
40  },
41  video: {
42    title: 'Out There Trailer',
43    body:
44      'By the European Southern Observatory.\n\nSource: https://www.eso.org/public/videos/OutThere_trailer_en/',
45    richContent: {
46      video: 'https://cdn.eso.org/videos/medium_podcast/OutThere_trailer_en.mp4',
47    },
48  },
49  imageWithCustomIcon: {
50    title: 'Jaguar head shot',
51    body:
52      'Potrait of a jaguar at the Milwaukee County Zoological Gardens in Milwaukee, Wisconsin.\n\nSource: https://commons.wikimedia.org/wiki/File:Jaguar_head_shot-edit2.jpg and https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Jaguar_head_icon.svg/600px-Jaguar_head_icon.svg.png',
53    richContent: {
54      image: 'https://upload.wikimedia.org/wikipedia/commons/c/c9/Jaguar_head_shot-edit2.jpg',
55    },
56    icon:
57      'https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Jaguar_head_icon.svg/600px-Jaguar_head_icon.svg.png',
58    data: {
59      binomialName: 'Panthera onca',
60    },
61  },
62};
63
64// In this test app we contact the Expo push service directly. You *never*
65// should do this in a real app. You should always store the push tokens on your
66// own server or use the local notification API if you want to notify this user.
67const PUSH_ENDPOINT = 'https://expo.io/--/api/v2/push/send';
68
69export default async function registerForPushNotificationsAsync(type: string) {
70  // this method assumes the user has already granted permission
71  // to receive remote notificartions.
72
73  // Get the token that uniquely identifies this device
74  const token = await Notifications.getExpoPushTokenAsync();
75
76  // Log it so we can easily copy it if we need to work with it
77  console.log(`Got this device's push token: ${token}`);
78
79  await Notifications.createCategoryAsync('welcome', [
80    {
81      actionId: 'tada',
82      buttonTitle: '��',
83      isDestructive: false,
84      isAuthenticationRequired: false,
85    },
86    {
87      actionId: 'heart_eyes',
88      buttonTitle: '��',
89      isDestructive: false,
90      isAuthenticationRequired: true,
91    },
92  ]);
93
94  // POST the token to the Expo push server
95  const response = await fetch(PUSH_ENDPOINT, {
96    method: 'POST',
97    headers: {
98      'Accept': 'application/json',
99      'Content-Type': 'application/json',
100    },
101    body: JSON.stringify([
102      {
103        to: token,
104        _category: `${Constants.manifest.id}:welcome`,
105        ...demoBodies[type],
106      },
107    ]),
108  });
109
110  const result = await response.json();
111  if (result.errors) {
112    for (const error of result.errors) {
113      console.warn(`API error sending push notification:`, error);
114    }
115  }
116
117  const receipts = result.data;
118  if (receipts) {
119    const receipt = receipts[0];
120    if (receipt.status === 'error') {
121      if (receipt.details) {
122        console.warn(
123          `Expo push service reported an error sending a notification: ${
124            receipt.details.error
125          }`
126        );
127      }
128      if (receipt.__debug) {
129        console.warn(receipt.__debug);
130      }
131    }
132  }
133}
134