使用 HTTP/2 API 确定何时在 expo 上发送推送通知
Determine when to send push notifications on expo with HTTP/2 API
我正在尝试构建一个水提醒应用程序。我有 3 个屏幕,我正在使用 react-navigation
- 首页(就是我让用户当天加量喝的)
- 通知(用户在需要时使用切换按钮定义
接收通知以及何时接收)
- 设置(用户输入年龄、体重以确定他们每天应该喝多少)。这是用户下载应用程序后看到的第一个屏幕
我正在尝试使用 expo 推送通知及其 HTTP/2 API 向我的用户发送推送通知。但是我对此有点迷茫,下面有这些问题。
- 下面的推送通知代码在哪里写调用HTTP/2API? (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,而是使用
Notifications.scheduleLocalNotificationAsync(localNotification,
schedulingOptions)
这有助于我安排本地通知在将来某个特定时间或给定时间间隔触发。
参数
localNotification (object) -- 具有 LocalNotification 中描述的属性的对象。
schedulingOptions (object) -- 描述通知何时触发的对象。
time(日期或数字) -- 表示何时触发
通知或 Unix 纪元时间的数字。示例:(新
Date().getTime() + 1000 是从现在开始的一秒。
重复(可选)(字符串) -- 'minute'、'hour'、'day'、'week',
'month',或'year'.
(仅Android) intervalMs (optional) (number) -- Repeat interval in
毫秒数
componentDidMount() {
this.willFocusSubscription = this.props.navigation.addListener('willFocus', 负载 => {
// 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);
}
});
};
我正在尝试构建一个水提醒应用程序。我有 3 个屏幕,我正在使用 react-navigation
- 首页(就是我让用户当天加量喝的)
- 通知(用户在需要时使用切换按钮定义 接收通知以及何时接收)
- 设置(用户输入年龄、体重以确定他们每天应该喝多少)。这是用户下载应用程序后看到的第一个屏幕
我正在尝试使用 expo 推送通知及其 HTTP/2 API 向我的用户发送推送通知。但是我对此有点迷茫,下面有这些问题。
- 下面的推送通知代码在哪里写调用HTTP/2API? (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,而是使用
Notifications.scheduleLocalNotificationAsync(localNotification, schedulingOptions)
这有助于我安排本地通知在将来某个特定时间或给定时间间隔触发。
参数
localNotification (object) -- 具有 LocalNotification 中描述的属性的对象。
schedulingOptions (object) -- 描述通知何时触发的对象。
time(日期或数字) -- 表示何时触发 通知或 Unix 纪元时间的数字。示例:(新 Date().getTime() + 1000 是从现在开始的一秒。
重复(可选)(字符串) -- 'minute'、'hour'、'day'、'week',
'month',或'year'.(仅Android) intervalMs (optional) (number) -- Repeat interval in
毫秒数componentDidMount() { this.willFocusSubscription = this.props.navigation.addListener('willFocus', 负载 => {
// 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);
}
});
};