【问题标题】:react native: check Android background activities settingreact native:检查 Android 后台活动设置
【发布时间】:2019-11-01 15:18:06
【问题描述】:

我希望我的应用程序检查其后台活动是否在 Android 设置中启用。

Settings => Apps & notifications => Advanced => Apps background activities

我知道react-native-device-info 包,但我没有看到任何与后台活动相关的方法。

我怎样才能做到这一点?

【问题讨论】:

    标签: android firebase react-native push-notification


    【解决方案1】:

    'react-native-device-info'不知道您的应用状态。此模块是您在需要它以获取有关该设备的信息时使用的模块。例如UUID值、APPID值等

    AppState可以告诉你应用是在前台还是后台,并在状态变化时通知你。

    • active - 应用正在前台运行
    • background - 应用程序正在后台运行。用户是 任何一个:
      • 在另一个应用中
      • 在主屏幕上
      • [Android] 在另一个 Activity 上(即使它是由您的 应用程序)

    示例

    import React, {Component} from 'react';
    import {AppState, Text} from 'react-native';
    
    class AppStateExample extends Component {
      state = {
        appState: AppState.currentState,
      };
    
      componentDidMount() {
        AppState.addEventListener('change', this._handleAppStateChange);
      }
    
      componentWillUnmount() {
        AppState.removeEventListener('change', this._handleAppStateChange);
      }
    
      _handleAppStateChange = (nextAppState) => {
        if (
          this.state.appState.match(/inactive|background/) &&
          nextAppState === 'active'
        ) {
          console.log('App has come to the foreground!');
        }
        this.setState({appState: nextAppState});
      };
    
      render() {
        return <Text>Current state is: {this.state.appState}</Text>;
      }
    }
    

    FCM Document

    后台受限应用(Android P 或更高版本)

    从 2019 年 1 月开始,FCM 不会将消息转发到具有后台限制的应用(例如,设置-> 应用和通知-> [appname] 电池)。当应用程序从后台限制中删除时,会像以前一样传递有关该应用程序的新消息。

    看看the part about "remote push notifications":

    【讨论】:

    • 实际上,我希望我的应用即使在关闭/杀死时也能收到通知。但是,如果我的应用程序后台活动设置被禁用(默认情况下),用户将永远不会知道通知。这就是为什么我希望我的应用程序在开始通知用户时检测其后台活动状态(以防它被禁用)。
    • 如果您说的通知是推送消息,即使在后台也可以收到推送消息。
    • 我知道,但是一旦您的应用程序关闭或终止,后台状态将不再可用。我花了很多时间来弄清楚为什么在应用程序关闭时我无法收到推送的消息。答案是我的应用后台活动在智能手机设置中被禁用,因此我提出了问题。
    猜你喜欢
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多