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.

PushNotification.configure doesn't work

See original GitHub issue

Hello,

Here is how I followed the reading is when I run the application I can not display the token because it does not fit in the PushNotification.configure present componentDidMount(). Can anybody help me? Thank you

import PushNotification from 'react-native-push-notification';

type Props ={};
export default class App extends Component<Props>{

  componentDidMount(){

    console.log('Did Mount');

    PushNotification.configure({
      onRegister:function(token) {
        console.log('TOKEN: ', token);
      },

      onNotification:function(notification) {
        console.log('NOTIFICATION: ',notification);
      },

      senderID: "826032976309",

      popInitialNotification: true,

      requestPermissions: true,
    });
  }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:27 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
GastonEDiazcommented, Jun 5, 2018

Hi!! I could solve it, placing PushNotification.configure() outside the component.

Example:

import React, { Component } from 'react';
import {  ActivityIndicator, Alert, AsyncStorage, Image, Linking, KeyboardAvoidingView, Platform, StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native';

var PushNotification = require('react-native-push-notification');

PushNotification.configure({

  // (optional) Called when Token is generated (iOS and Android)
  onRegister: function(token) {
    if(token.token != undefined)
    {
      this.token = token.token;
    }
  },

  // (required) Called when a remote or local notification is opened or received
  onNotification: function(notification) {
      if(notification.foreground)
      {
        Alert.alert(notification.message);
      }
      console.log(notification);
      // required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
      if(Platform.OS == 'ios')
      {
        notification.finish(PushNotificationIOS.FetchResult.NoData);
      }
  },

  // ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
  senderID: '',
  token: '',
  // IOS ONLY (optional): default: all - Permissions to register.
  permissions: {
      alert: true,
      badge: true,
      sound: true
  },

  // Should the initial notification be popped automatically
  // default: true
  popInitialNotification: true,

  /**
   * (optional) default: true
   * - Specified if permissions (ios) and token (android and ios) will requested or not,
   * - if not, you must call PushNotificationsHandler.requestPermissions() later
   */
  requestPermissions: true,
});

export default class LoginForm extends Component {
  
  constructor(props) {
    super(props);    
  }

render() {
console.log(PushNotification.token); //Don't working in emulator only in pysical device
return(
<View>
<Text>Hello</Text>
</View>
);
}
4reactions
LongicornYucommented, Jul 26, 2018

I was experiencing the same issue when sending local notifications to a simulator, the onNotificaiton is not fired. Works fine for real device.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React native Push Notification onNotification event not working
PushNotification.configure({ // (required) Called when a remote or local notification is opened or received onNotification: notification ...
Read more >
How to use the react-native-push-notification.configure ... - Snyk
To help you get started, we've selected a few react-native-push-notification.configure examples, based on popular ways it is used in public projects.
Read more >
Troubleshooting Duo Push notification issues on iOS devices
To check if push notifications are enabled for Duo Mobile and re-enable them if needed, follow these steps: On the iPhone, open Settings....
Read more >
Step by Step guide for Implementing Push Notifications in iOS ...
Implementing Push Notification was a nightmare as we had a tough time ... If you do not already have an account you can...
Read more >
Notification Troubleshooting - PagerDuty Knowledge Base
You may wish to reset your push notification settings and preferences in ... Due to a Work Profile limitation from Android, overriding Do...
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