gpt4 book ai didi

react-native - 本地计划通知 native 响应

转载 作者:行者123 更新时间:2023-12-03 16:18:51 25 4
gpt4 key购买 nike

我是React Native的新手,我正在尝试实现一项功能,该应用程序每天在特定的日期和时间段向用户发送通知,以提醒用户服用药丸(时间和日期和数据存储在mongodb服务器中,因此我将会使用axios来获取它们)
如果应用关闭或在后台,通知仍然可以正常工作吗?
使它工作容易吗?
有谁知道实现这一目标的方法,这可能吗?

谢谢

最佳答案

是的!!!这个有可能。您有许多选择,其中包括:

1)在iOS和Android中使用HeadlessTask进行后台抓取,这是一个不错的库
https://github.com/jamesisaac/react-native-background-task

2)推送来自服务器的通知,以确保确定地唤醒该应用程序(除非该应用程序已被操作系统杀死)。在iOS中,请确保调用notification.finish()以避免被任务处理程序算法所区别。

一个简单的没有1的例子是这样的(未测试!):

import React from 'react'
import { Text } from 'react-native'
import BackgroundTask from 'react-native-background-task'
import { Notifications, Permissions, Constants } from 'expo';

BackgroundTask.define(async () => {

// if time is 12pm, fire off a request with axios to fetch the pills info
const response = await fetch('http://pills-server')


const text = await response.text()

// Data persisted to AsyncStorage can later be accessed by the foreground app
await AsyncStorage.setItem('@MyApp:key', text)

// Notification configuration object
const localNotification = {
title: text,
body: 'msg',
data: data,
ios: {
sound: true
}
}

// trigger notification, note that on ios if the app is open(in foreground) the notification will not show so you will need to find some ways to handling it which is discribed here https://docs.expo.io/versions/latest/guides/push-notifications

Notifications
.presentLocalNotificationAsync(localNotification)
.catch((err) => {
console.log(err)
})


BackgroundTask.finish()
})

class MyApp extends React.Component {

async componentDidMount() {
// allows the app to recieve notifications (permission stuff)
this.registerForPushNotificationsAsync().then(() => {
BackgroundTask.schedule()
});

}

registerForPushNotificationsAsync = async () => {

const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
if (status !== 'granted') {
return;
}
let deviceToken = await Notifications.getExpoPushTokenAsync()

}



render() {
return <Text>Hello world</Text>
}
}

对于第二个问题,想法是服务器应具有某种cron作业的功能,该作业可以定期地根据所有用户设备的不同时间/日期和存储在数据库中的药丸信息向所有用户设备发送通知。

用于博览会的服务器端实现的N.B可以将 https://github.com/expo/expo-server-sdk-node用于node.js项目,此处列出了其他SDK: https://docs.expo.io/versions/latest/guides/push-notifications/
import React from 'react'; 
import { Notifications, Permissions, Constants } from 'expo';
import { Text, View } from 'react-native'

class App extends React.Component {

registerForPushNotificationsAsync = async () => {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
// prevent device from registering more than once
if (status !== 'granted') {
return;
}

// get unique token of the device that the server can use to push notification to
the device
let deviceToken = await Notifications.getExpoPushTokenAsync();

// call a method that sends the device token to the server, in which server stores somewhere and uses in the future
this.props.registerToken({ deviceToken }).then(() => {

// the listener here, is called whenever a push notification comes from the server or when the user clicks on the notification at the device tray
this.notificationSubscription =
Notifications.addListener(this.handleNotification);
})
}

handleNotification = (notification) => {

if (notification.origin === 'selected') {
// The notification was clicked from the tray by the user i.e selected
// do stuff to handle selection
} else {
// The notification originated from the server
const localNotification = {
title: notification.title,
body: notification.message,
data: data,
ios: {
sound: true
}
}
Notifications.presentLocalNotificationAsync(localNotification).catch((err) => {
console.log(err)
})
}

}

async componentDidMount() {

this.registerForPushNotificationsAsync().then(() => {
});

}


render() {
return (
<View>
<Text>Helllo world</Text>
</View>
);
}
}

请注意,这些解决方案尚未经过测试,可能包含一些错误,但是总体思路仍然相同,可以进行调整:)。

希望能帮助到你。

关于react-native - 本地计划通知 native 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56092937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com