-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notifications.js
62 lines (52 loc) · 1.56 KB
/
Notifications.js
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
import Constants from "expo-constants";
import { Notifications } from "expo";
import * as Permissions from "expo-permissions";
export const loadNotifications = () => {
askPermission();
Notifications.scheduleLocalNotificationAsync(
localNotification,
schedulingOptions
);
return Notifications.addListener(notificationHandler);
};
const notificationHandler = () => {
console.log("Notification received!");
};
const localNotification = {
sound: "default",
title: "Getting Hungry?",
body: "Why not try searching for dinner ideas?",
ios: { _displayInForeground: true },
};
const schedulingOptions = {
time: new Date().setHours(17, 0, 0),
};
const askPermission = async () => {
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== "granted") {
alert("Failed to get push token for push notification!");
return;
}
console.log("Permission was granted!");
// only need token for push notifications
// let token = await Notifications.getExpoPushTokenAsync();
} else {
alert("Must use physical device for Push Notifications");
}
if (Platform.OS === "android") {
Notifications.createChannelAndroidAsync("default", {
name: "default",
sound: true,
priority: "max",
vibrate: [0, 250, 250, 250],
});
}
};