react-native-push-notification: PushNotification.configure doesn't work

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,
    });
  }

About this issue

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

Most upvoted comments

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>
);
}

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

Hi @aakrandan The README explain how to handle this, read the Usage Part, requestPermissions. Regards