expo notification django sdk

Solutions on MaxInterview for expo notification django sdk by the best coders in the world

showing results for - "expo notification django sdk"
Matilda
08 Feb 2016
1import Constants from 'expo-constants';
2import * as Notifications from 'expo-notifications';
3import React, { useState, useEffect, useRef } from 'react';
4import { Text, View, Button, Platform } from 'react-native';
5
6Notifications.setNotificationHandler({
7  handleNotification: async () => ({
8    shouldShowAlert: true,
9    shouldPlaySound: false,
10    shouldSetBadge: false,
11  }),
12});
13
14export default function App() {
15  const [expoPushToken, setExpoPushToken] = useState('');
16  const [notification, setNotification] = useState(false);
17  const notificationListener = useRef();
18  const responseListener = useRef();
19
20  useEffect(() => {
21    registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
22
23    // This listener is fired whenever a notification is received while the app is foregrounded
24    notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
25      setNotification(notification);
26    });
27
28    // This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
29    responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
30      console.log(response);
31    });
32
33    return () => {
34      Notifications.removeNotificationSubscription(notificationListener);
35      Notifications.removeNotificationSubscription(responseListener);
36    };
37  }, []);
38
39  return (
40    <View
41      style={{
42        flex: 1,
43        alignItems: 'center',
44        justifyContent: 'space-around',
45      }}>
46      <Text>Your expo push token: {expoPushToken}</Text>
47      <View style={{ alignItems: 'center', justifyContent: 'center' }}>
48        <Text>Title: {notification && notification.request.content.title} </Text>
49        <Text>Body: {notification && notification.request.content.body}</Text>
50        <Text>Data: {notification && JSON.stringify(notification.request.content.data)}</Text>
51      </View>
52      <Button
53        title="Press to Send Notification"
54        onPress={async () => {
55          await sendPushNotification(expoPushToken);
56        }}
57      />
58    </View>
59  );
60}
61
62// Can use this function below, OR use Expo's Push Notification Tool-> https://expo.io/notifications
63async function sendPushNotification(expoPushToken) {
64  const message = {
65    to: expoPushToken,
66    sound: 'default',
67    title: 'Original Title',
68    body: 'And here is the body!',
69    data: { someData: 'goes here' },
70  };
71
72  await fetch('https://exp.host/--/api/v2/push/send', {
73    method: 'POST',
74    headers: {
75      Accept: 'application/json',
76      'Accept-encoding': 'gzip, deflate',
77      'Content-Type': 'application/json',
78    },
79    body: JSON.stringify(message),
80  });
81}
82
83async function registerForPushNotificationsAsync() {
84  let token;
85  if (Constants.isDevice) {
86    const { status: existingStatus } = await Notifications.getPermissionsAsync();
87    let finalStatus = existingStatus;
88    if (existingStatus !== 'granted') {
89      const { status } = await Notifications.requestPermissionsAsync();
90      finalStatus = status;
91    }
92    if (finalStatus !== 'granted') {
93      alert('Failed to get push token for push notification!');
94      return;
95    }
96    token = (await Notifications.getExpoPushTokenAsync()).data;
97    console.log(token);
98  } else {
99    alert('Must use physical device for Push Notifications');
100  }
101
102  if (Platform.OS === 'android') {
103    Notifications.setNotificationChannelAsync('default', {
104      name: 'default',
105      importance: Notifications.AndroidImportance.MAX,
106      vibrationPattern: [0, 250, 250, 250],
107      lightColor: '#FF231F7C',
108    });
109  }
110
111  return token;
112}
113
similar questions
queries leading to this page
expo notification django sdk