gpt4 book ai didi

react-native - 博览会-本地通知在设备上不起作用

转载 作者:行者123 更新时间:2023-12-04 04:28:26 24 4
gpt4 key购买 nike

我正在使用EXPO和React Native为Android和iOS创建一个应用程序。
此代码中有一个奇怪的行为:它在Expo App上可以正常工作,但在真实设备(独立版本)(在iOS和Android上发生)中却无法工作。

这些本地通知的逻辑如下:
这个想法是向用户显示3条本地通知:

  • 用户上次打开应用程序
  • 之后的第一个星期,即第一个星期
  • 第二个,用户最后一次打开应用程序后的1个月
  • 用户最后一次打开应用程序
  • 之后的最后两个月

    我该怎么做?

    第一次打开应用程序时,我问用户是否要查看通知。如果用户说是,我将该信息写入本地存储中,然后请求 USER_FACING_NOTIFICATIONS权限以显示本地通知。

    我正在使用 USER_FACING_NOTIFICATIONS,因为我不发送远程通知。

    然后,用户第二次打开该应用程序,请执行以下操作:
  • 我删除了所有以前的通知
  • 我检查用户是否想要查看通知
  • 我检查用户/设备是否授予我们显示通知的权限
  • 我安排了3条通知。

  • 在这里,部分代码:

    componentDidMount() {
    cancelAllScheduled()
    .then(() => {
    getUserNotificationsPermission().then(userWants => {
    if (userWants) {
    getDeviceNotificationsPermission().then(grantedByDevice => {
    if (grantedByDevice) {
    scheduleNotifications();
    }
    });
    }
    });
    })
    .catch(() => {
    // :shrug:
    });
    }

    在这里,所有这些 utils函数:

    // Cancel all the notifications
    const cancelAllScheduled = () => {
    return ExpoNotifications.cancelAllScheduledNotificationsAsync();
    }

    // Check in local storage if the user WANTS to see notifications
    const getUserNotificationsPermission = () => {
    return AsyncStorage.getItem('userNotificationPermission').then(data => {
    let isEnabled;

    try {
    isEnabled = JSON.parse(data);
    } catch (error) {
    isEnabled = false;
    }

    return isEnabled;
    });
    };

    // Check if user/device ALLOW US to show notifications
    const getDeviceNotificationsPermission = () => {
    return Permissions.getAsync(Permissions.USER_FACING_NOTIFICATIONS).then(({ status }) => {
    return status === 'granted';
    });
    };

    // Add days
    // https://stackoverflow.com/a/19691491/1815449
    const _addDays = (date, days) => {
    var result = new Date(date);
    result.setDate(result.getDate() + days);
    return result;
    };

    // Schedule all the notifications
    // One notification is going to be shown on 1 week
    // Other notification in 1 month
    // Other notification in 2 month
    const scheduleNotifications = () => {
    const now = new Date();

    const oneWeek = {
    title: 'We miss you!',
    body: "Come to see what we've been up to and share your latest adventures with us!",
    data: {
    type: 'WEEKLY',
    },
    };
    const oneWeekOptions = { time: _addDays(now, 7) };
    ExpoNotifications.scheduleLocalNotificationAsync(oneWeek, oneWeekOptions);

    const oneMonth = {
    title: 'We miss you!',
    body: "Come to see what we've been up to and share your latest adventures with us!",
    data: {
    type: 'MONTHLY',
    },
    };
    const oneMonthOptions = { time: _addDays(now, 30) };
    ExpoNotifications.scheduleLocalNotificationAsync(oneMonth, oneMonthOptions);

    const twoMonth = {
    title: 'We miss you!',
    body: "Come to see what we've been up to and share your latest adventures with us!",
    data: {
    type: 'MONTHLY 2',
    },
    };
    const twoMonthOptions = { time: _addDays(now, 60) };
    ExpoNotifications.scheduleLocalNotificationAsync(twoMonth, twoMonthOptions);
    };

    您是否看到任何有关为何无法正常工作的线索?
    我已经打开了该应用程序并授予了所有权限,然后杀死并再次打开该应用程序,接近1.000次。最近几个月,我没有打开该应用程序9天(我做了3次),但从未收到任何通知。但是,通知会正确显示在Expo Dev App中。

    附注:我每次用户打开应用程序时都要执行 cancelAllScheduled,因为每次用户打开应用程序时都需要重置这些通知,原因是我想显示“上次打开应用程序”之后的几周

    Ps2:这是我执行该文档所遵循的文档:
  • https://docs.expo.io/versions/latest/sdk/notifications/
  • https://docs.expo.io/versions/v32.0.0/sdk/permissions/
  • 最佳答案

    似乎是问题所在:https://github.com/expo/expo/issues/4121
    通知有效,但由于我将它们安排了1周,1个月或更长时间,因此未显示通知,原因是大多数设备在安排通知后都已重新启动。

    我已经等待了1周,没有在iOS中再次打开该应用,也没有重新启动设备甚至没有升级SO,我可以看到通知。
    我尚未在Android中进行测试。我可能不得不将Android手机保持连接状态1周,然后看看会发生什么情况。

    关于react-native - 博览会-本地通知在设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56134962/

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