问题描述
我想要这样的行为......我在我的应用程序中添加了 expo-Notification,它可以正常工作并反复向我发送通知但是当我正确或完全关闭或崩溃我的应用程序时,它无法向我发送通知,那就是太好了...现在主要的问题是...当我完全关闭我的应用程序时,我也想收到通知...我该怎么做?...我没有登录或数据库附加...只是发送重复点击按钮上的通知但是当我只点击一次按钮时,它会在 5 秒后重复向我发送通知(无需一次又一次按下按钮)这里我的问题在我完全关闭应用程序时到达,我也想一次又一次地收到通知.... ..... 尽快帮助我或向我提出任何建议...它真的对我的项目有帮助...
这是我的代码......
import Constants from "expo-constants";
import * as Notifications from "expo-notifications";
import React,{ useState,useEffect,useRef } from "react";
import { Text,View,Button,Platform } from "react-native";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,shouldplaySound: false,shouldSetBadge: false,}),});
export default function App() {
const [expoPushToken,setExpoPushToken] = useState("");
const [notification,setNotification] = useState(false);
const notificationListener = useRef();
const responseListener = useRef();
useEffect(() => {
registerForPushNotificationsAsync().then((token) =>
setExpoPushToken(token)
);
notificationListener.current = Notifications.addNotificationReceivedListener(
(notification) => {
setNotification(notification);
}
);
responseListener.current = Notifications.addNotificationResponseReceivedListener(
(response) => {
console.log(response);
}
);
return () => {
Notifications.removeNotificationSubscription(notificationListener);
Notifications.removeNotificationSubscription(responseListener);
};
});
return (
<View
style={{
flex: 1,alignItems: "center",justifyContent: "space-around",}}
>
<Text>Your expo push token: {expoPushToken}</Text>
<View style={{ alignItems: "center",justifyContent: "center" }}>
<Text>
Title: {notification && notification.request.content.title}{" "}
</Text>
<Text>Body: {notification && notification.request.content.body}</Text>
<Text>
Data:{" "}
{notification && JSON.stringify(notification.request.content.data)}
</Text>
</View>
<Button
title="Press to schedule a notification"
onPress={async () => {
await schedulePushNotification();
}}
/>
</View>
);
}
async function schedulePushNotification() {
await Notifications.scheduleNotificationAsync({
content: {
title: "You've got mail! ?",body: "Here is the notification body",data: { data: "goes here" },},trigger: {
seconds: 5,repeats: true,});
}
async function registerForPushNotificationsAsync() {
let token;
if (Constants.isDevice) {
const {
status: existingStatus,} = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== "granted") {
alert("Failed to get push token for push notification!");
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(token);
} else {
alert("Must use physical device for Push Notifications");
}
if (Platform.OS === "android") {
Notifications.setNotificationChannelAsync("default",{
name: "default",importance: Notifications.AndroidImportance.MAX,vibrationPattern: [0,250,250],lightColor: "#FF231F7C",});
}
return token;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)