微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我的应用程序完全崩溃或完全关闭时,我想添加功能以在我的 React 原生应用程序中接收通知,我想要这种行为

如何解决当我的应用程序完全崩溃或完全关闭时,我想添加功能以在我的 React 原生应用程序中接收通知,我想要这种行为

我想要这样的行为......我在我的应用程序中添加了 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?