我正在尝试构建一个水提醒应用程序。我有 3 个屏幕,我正在使用 react-navigation
- 家(我允许用户增加他们当天的饮料量)
- 通知(用户使用开关按钮定义是否要接收通知以及何时接收)
- 设置(用户输入年龄、体重以确定他们每天应该喝多少)。这是用户下载应用程序时看到的第一个屏幕
我正在尝试使用 expo 推送通知及其 HTTP/2 API 向我的用户发送推送通知。但是我对此有点迷茫,并且在下面有这些问题。
- 下面在哪里编写推送通知代码并调用 HTTP/2 API?(App.js、通知或设置?)
- 如何根据用户选择确定何时发送此通知,例如每小时发送一次。
我的获取权限、存储密钥和调用 api 发送通知的代码。
registerforPushNotifications = async () => {
// check fox existing permission
const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = status;
// if no existing permission granted ask user
if (status !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
//if no permission is granted exit the status
if (finalStatus !== 'granted') {
return;
}
// get the token
let token = await Notifications.getExpoPushTokenAsync();
const request = {
to: token,
title: "Don't forget to drink water",
};
_storeToken = async () => {
try {
await AsyncStorage.setItem('token', token.toString());
} catch (error) {
console.log(error.message);
}
};
_storeToken();
return fetch('https://exp.host/--/api/v2/push/send', {
method: 'POST',
headers: {
'host':'exp.host',
'accept': 'application/json',
'content-Type': 'application/json',
'accept-encoding': 'gzip, deflate'
},
body: JSON.stringify(request),
}).then(data => {console.log(data)});
};
我收到的回复
"_bodyInit": "{\"data\":{\"status\":\"ok\",\"id\":\"93d0f654-d8f6-4587-b4bb-ed4f5cd08b68\"}}",