gpt4 book ai didi

react-native - 如何在后台显示本地通知

转载 作者:行者123 更新时间:2023-12-03 02:28:54 25 4
gpt4 key购买 nike

我正在尝试设置具有一定时间的本地通知,以便在时间到时为用户推送通知。
我需要两个步骤的帮助:

1)如何在应用程序处于后台状态时触发通知

2)如何在触发时向通知添加声音

我也想知道我使用的方法是否正确?

import React, {useEffect, useState} from 'react';
import { Text, View, Button, Vibration, Platform , Alert} from 'react-native';
import {Notifications} from 'expo';
import * as Permissions from 'expo-permissions';
import Constants from 'expo-constants';



const LocalNotificationsScreen = () => {

const [notification, setNotification] = useState({});


useEffect(() => {
askPermissions();
}, []);


let Times = [
{time: '3:16' , name : 'test1'}, {time : '0:7', name : 'test2'},
{time : '0:9', name : 'test3'}, {time : '2:8' , name : 'test4'},
{time : '3:37' , name : 'test5'} , {time : '3:39', name : 'test6'}
];


const askPermissions = async () => {
const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== 'granted') {
return false;
}
return true;
};

const localNotification = {
id : 1,
title : 'Testing',
body : 'The body ',
data: { name : 'This is the data'},
ios : {
sound : true,
_displayInForeground : true
},
android : {
name : 'Sound',
sound : true,
vibrate : [ 0, 250, 250, 250 ],
repeat : false
},
userText: 'Hi from Notfications'
}


const schedulingOptions = {
time : (new Date()).getTime() + 5000,
}

console.log((new Date()).getTime())

const currentTime = `${new Date().getHours()}:${new Date().getMinutes()}`;
console.log(currentTime);



const pushTime = () => {
return Times.map((t) => {
if (t.time === currentTime) {
let notificationId =
Notifications.scheduleLocalNotificationAsync(localNotification, schedulingOptions);
console.log(notificationId);
setTimeout(function () {
Notifications.cancelAllScheduledNotificationsAsync()
}, 8000);
}
})
}

pushTime();




Notifications.addListener(
notification => {
Vibration.vibrate();
console.log(notification);
setNotification(notification);
}
)

return (
<View>
<Text>LocalNotifications </Text>
</View>
);
}


export default LocalNotificationsScreen;

最佳答案

我对expo不了解,但是通过使用此程序包在我的应用程序中使用了本地通知。
检查本地通知部分...我希望它可以与博览会合作。
https://github.com/zo0r/react-native-push-notification

关于react-native - 如何在后台显示本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61945588/

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