我正在尝试构建一个水提醒应用程序。我有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"}}",
而不是使用firebase,我决定使用
通知 调度)
这有助于我安排本地通知在将来或给定间隔的某个特定时间发射。
参数
localnotification(object) - 具有localnotification中属性的对象。
schedulingOptions(object) - 一个描述通知何时发射的对象。
-
时间(日期或号码) - 代表何时启动的日期对象通知或UNIX时期时间的数字。示例:(新date())。getTime() 1000从现在起一秒钟。
-
重复(可选)(string)(字符串) - 'minute','hour','day','','周,
"月"或"年"。
态毫秒数
componentDidmount(){ this.willfocussubscription = this.props.navigation.addlistener('willfocus',paryload => {
// call the functions after component did mounted
this._handleLocalNotification();
this.listenForNotifications();
});}
// function to request permission to send notifications and schedule notifications
_handleLocalNotification = 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;
}
const localnotification = {
title: 'Water Reminder',
body: 'Dont forget to drink water!',
android: {
sound: true,
},
ios: {
sound: true,
},
};
let sendAfterFiveSeconds = Date.now();
sendAfterFiveSeconds += 5000;
const schedulingOptions = { time: sendAfterFiveSeconds };
Notifications.scheduleLocalNotificationAsync(localnotification, schedulingOptions);
};
//函数可以在打开应用程序时收到通知(如果收到的通知)。收到时,它将创建并提醒
listenForNotifications = () => {
Notifications.addListener(notification => {
if (notification.origin === 'received') {
Alert.alert(localnotification.title, localnotification.body);
}
});
};