user
user

Reputation: 3199

How to make notification lights work in react-native on android?

I have a simple react-native camera app and I would like to make notification LED light up at the bottom of the phone, when app is recording. I was not able to find it on the official docs.

I removed unnecessary code (like the styles and template) for ease of readability. My index.android.js is the following.

import React from 'react';
import {
  View,
  Image,
  StatusBar,
  StyleSheet,
  AppRegistry,
  TouchableOpacity,
} from 'react-native';

import Camera from 'react-native-camera';

const styles = StyleSheet.create({
    //...
});

export default class DashCam extends React.Component {
  constructor(props) {
    super(props);

    this.camera = null;

    this.state = {
      camera: {
        aspect: Camera.constants.Aspect.fill,
        captureTarget: Camera.constants.CaptureTarget.cameraRoll,
        type: Camera.constants.Type.back,
        orientation: Camera.constants.Orientation.auto,
      },
      isRecording: false
    };
    this.switchCam = this.switchCam.bind(this);
    this.recording = this.recording.bind(this);
  }

  recording() {
    console.log(!this.state.isRecording);
    if(!this.state.isRecording) {
      if (this.camera) {
        this.camera.capture({mode: Camera.constants.CaptureMode.video})
            .then((data) => console.log(data))
            .catch(err => console.error(err));

        this.setState({ isRecording: true });
      }
      console.log('recording');
    } else {
      if (this.camera) {
        this.camera.stopCapture();
        this.setState({ isRecording: false });
      }
      console.log('stopped ');
    }
  }

  switchCam() {
    //...
  }

  get typeIcon() {
    //...
  }

  get camButton() {
    //...
  }

  render() {
    return (
        //...
    );
  }
}

AppRegistry.registerComponent('DashCam', () => DashCam);

My package.json if you need it:

{
  "name": "DashCam",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "15.3.2",
    "react-native": "0.37.0",
    "react-native-camera": "git+https://github.com/lwansbrough/react-native-camera.git"
  },
  "jest": {
    "preset": "jest-react-native"
  },
  "devDependencies": {
    "babel-jest": "17.0.2",
    "babel-preset-react-native": "1.9.0",
    "jest": "17.0.3",
    "jest-react-native": "17.0.3",
    "react-test-renderer": "15.3.2"
  }
}

Upvotes: 4

Views: 1556

Answers (1)

martinarroyo
martinarroyo

Reputation: 9701

As you point out, this functionality is not included in RN, but the good thing is you can easily implement it yourself in Android code. Maybe something like this can help you turn the LED on/off (by basically creating a dummy notification) and then you can build an Android module, which is actually fairly simple. You can have a look at the Toast tutorial in the official docs.

Upvotes: 2

Related Questions