博览会-本地通知无法在设备上运行

时间:2019-05-14 16:38:32

标签: react-native notifications expo

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

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

  • 第一个,用户最后一次打开应用后1周
  • 第二次,用户最后一次打开应用后的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中。

Ps .:每次用户打开应用程序时,我都会执行cancelAllScheduled,因为每次用户打开应用程序时都需要重置这些通知,因为我想显示“最后一次应用程序”之后的几周已打开”

Ps2 .:这是我执行该文档所遵循的文档:

1 个答案:

答案 0 :(得分:2)

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

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

相关问题