React native 确定何时使用HTTP/2 API在expo上发送推送通知
我正在尝试建立一个水提醒应用程序。我有3个屏幕,我正在使用react导航React native 确定何时使用HTTP/2 API在expo上发送推送通知,react-native,react-native-android,expo,react-native-push-notification,React Native,React Native Android,Expo,React Native Push Notification,我正在尝试建立一个水提醒应用程序。我有3个屏幕,我正在使用react导航 家(我允许用户增加当天的饮酒量) 通知(如果用户需要,可以使用开关按钮进行定义) 接收通知和接收时间) 设置(用户输入年龄、体重以确定他们每天应该喝多少)。这是用户下载应用程序时看到的第一个屏幕 我正在尝试使用expo推送通知及其HTTP/2 API向我的用户发送推送通知。但我有点迷茫,下面有这些问题 在何处编写推送通知代码并调用HTTP/2 API?(App.js,通知还是设置?) 如何根据用户选择确定何时发送此通知
- 家(我允许用户增加当天的饮酒量)
- 通知(如果用户需要,可以使用开关按钮进行定义) 接收通知和接收时间)
- 设置(用户输入年龄、体重以确定他们每天应该喝多少)。这是用户下载应用程序时看到的第一个屏幕
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而不是firebase scheduleLocalNotificationAsync(localNotification, 日程安排(可选) 这有助于我安排本地通知在未来某个特定时间或给定的时间间隔发出 参数 localNotification(对象)——具有localNotification中描述的属性的对象 调度选项(对象)——描述何时触发通知的对象
- 时间(日期或数字)——表示何时触发事件的日期对象 Unix纪元时间内的通知或数字。示例:(新) 日期())。getTime()+1000是从现在开始的一秒钟
- 重复(可选)(字符串)“分钟”、“小时”、“天”、“周”和
“月”或“年” - (仅限Android)间隔(可选)(数字)——重复
毫秒数 componentDidMount(){ this.willFocusSubscription=this.props.navigation.addListener('willFocus',payload=>{
//函数用于在应用程序打开时侦听是否收到通知。当收到通知时,它将创建并发出警报// 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); };
我决定使用firebase而不是firebase scheduleLocalNotificationAsync(localNotification, 日程安排(可选) 这有助于我安排本地通知在未来某个特定时间或给定的时间间隔发出 参数 localNotification(对象)——具有localNotification中描述的属性的对象 调度选项(对象)——描述何时触发通知的对象
- 时间(日期或数字)——表示何时触发事件的日期对象 Unix纪元时间内的通知或数字。示例:(新建) 日期())。getTime()+1000是从现在开始的一秒钟
- 重复(可选)(字符串)“分钟”、“小时”、“天”、“周”和
“月”或“年” - (仅限Android)间隔(可选)(数字)——重复
毫秒数 componentDidMount(){ this.willFocusSubscription=this.props.navigation.addListener('willFocus',payload=>{
//函数用于在应用程序打开时侦听是否收到通知。当收到通知时,它将创建并发出警报// 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); };