【问题标题】:React Native check if app notification enabled on AndroidReact Native 检查是否在 Android 上启用了应用通知
【发布时间】:2018-03-01 03:52:09
【问题描述】:

用户可以在 Android 上关闭应用通知。有没有办法检查用户是否这样做?

React Native doc 没有提及通知权限,因为它不是需要提示用户的权限。

Android docs 确实提到了访问通知策略的能力。

【问题讨论】:

  • 如果你能在android里做,你可以在RN里做。只需编写一个调用适当 API 的本机模块

标签: android react-native android-notifications


【解决方案1】:

我找到了一个名为React-native-permission-settings的库

它仅适用于 android,所以如果您需要 iOS 支持,我认为您也需要对其进行修改。

您可以按照here的安装指南进行操作

并在您的代码中实现它

import NotificationSettings from 'react-native-permission-settings';

...

NotificationSettings.areNotificationsEnabled((isEnabled: boolean) => {
  console.log(`Notifications are enabled: ${isEnabled}`);
});

【讨论】:

  • 从 RN 0.55 和 Android 8.0 开始,该库不再工作。还有其他推荐吗?
【解决方案2】:

我在以前的经验中使用过这个,效果很好:

https://github.com/jigaryadav/react-native-check-notification-enable

基本上我最终作为助手实现的是这两个函数:

export const getNotificationStatus = async () => {
  if (Platform.OS === 'ios') {
    const status = await Permissions.check(PermissionRequested.NOTIFICATION)
    if (status === PermissionsStatus.AUTHORIZED) {
      return true
    } else {
      return false
    }
  } else {
    // This is where I am using the library
    const status = await NotificationManager.areNotificationsEnabled()
    return status
  }
}

export const openNotificationsSettings = () => {
  if (Platform.OS === 'ios') {
    Linking.openURL('app-settings:')
  } else {
    AndroidOpenSettings.appNotificationSettings()
  }
}

只是为了解释一下整个sn-p:

我还结合使用这些库来实现所有功能:检查它们是否已启用并导航到设置页面以在需要时撤消

import AndroidOpenSettings from 'react-native-android-open-settings'
import NotificationManager from 'react-native-check-notification-enable'
import Permissions from 'react-native-permissions'

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 1970-01-01
    • 2022-07-22
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 2021-11-06
    相关资源
    最近更新 更多