question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Local Notification is not working on background

See original GitHub issue

My code is shown below

PushNotification.configure({
  onNotification: (notification) => {
    Actions.notifications();
  },
  popInitialNotification: true,
  requestPermissions: true,
});

componentDidMount() {
if (Platform.OS === 'android') {
      setTimeout(() => {
        PushNotification.localNotification({
          title: 'Something happened',
          message: 'Notification Message',
          foreground: false,
          userInteraction: true,
        });
      }, 5000);
    }
}

Platform : Android RN : 0.46.4 RN Notification: 3.0.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12

github_iconTop GitHub Comments

2reactions
sitompulcommented, Jan 23, 2018

@bwoodlt it looks like that you did not follow my instruction do not put it inside a react native lifecycle method your main component code supposed to be like this

import PushNotification from 'react-native-push-notification';
import { AppState } from 'react-native';
import React from 'react';

const onNotification = notification => {
  // Your on notification function
};

PushNotification.configure({
  onNotification,
  popInitialNotification: true,
  requestPermissions: true,
});

const appStateListener = (state) => {
  if (state === 'active') {
    PushNotification.popInitialNotification((notification) => {
      if (notification) {
        onNotification(notification);
      }
    });
  }
};

AppState.addEventListener('change', appStateListener);

class MainComponent extends React.Component {
  componentDidMount() {
    //some action
  }
  render() {
    //your render method
  }
}

export default MainComponent;
0reactions
jenalgitcommented, Oct 17, 2019

@RailtonMatthew add this to your main React Component

import PushNotification from 'react-native-push-notification';
import { AppState } from 'react-native';

const onNotification = notification => {
  // Your on notification function
};

PushNotification.configure({
  onNotification,
  popInitialNotification: true,
  requestPermissions: true,
});

const appStateListener = (state) => {
  if (state === 'active') {
    PushNotification.popInitialNotification((notification) => {
      if (notification) {
        onNotification(notification);
      }
    });
  }
};

AppState.addEventListener('change', appStateListener);

Bang @sitompul ada tutorialnya ga, baru di RN nih… butuh notif pas lagi ga dibuka appnya

Read more comments on GitHub >

github_iconTop Results From Across the Web

Local Notification not working in Background Mode
When app is in background, notifications wont show. After investigating this issue i found that they work fine when app is running in...
Read more >
Flutter Local Notification not working in background?
I am using flutter local notification to display schedule notification in my app. But unfortunately it is not working when app is in ......
Read more >
awesome_notifications | Flutter Package - Pub.dev
A complete solution to create Local and Push Notifications, customizing buttons, images, sounds, emoticons and applying many different layouts for Flutter ...
Read more >
Quick guide on local notifications for iOS - tanaschita.com
When the app is running in the foreground, the notification will not be shown to the user by default. To show the notification...
Read more >
Background fetch for local notifications : r/iOSProgramming
The only solutions that comes to mind would be to abuse background fetches and use that time to schedule local notifications or use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found