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

Created on 5 Jun 2018  ·  27Comments  ·  Source: zo0r/react-native-push-notification

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,
    });
  }
Stale help wanted question

Most helpful comment

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

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

Thank for you help.

+1 - It's not working for me either. The onNotification and onRegister are not getting triggered for local notifications... Any idea's on how to fix this ?

@ravindra4me What's your environment ? (OS, Simulator/Physical device, Version of package, version of RN)

I had the same problem and I followed the answer of @GastonEDiaz ans still notifications doesn't come but compilation is done without any problem.
Is there anything more that I have to do?

I have the same problem, message is sent by server but iOS device not receive this message.

Can you check the provisioning profiles you are using ?
What's the payload of your notification ?

I see logs in device logs from APS but method is not triggered.
Eg. payload:

Sent message Message(Id=2; Token=...; Payload={"aps":{"alert":{"title":"TTt","body":"Can't be simpler than this!"}}}) Resent: false

Your payload indeed seems to be OK.
What's the log you see from APS ?

wartość domyślna 18:15:46.801894 +0200 apsd Created power assertion {identifier: APSCourier(tcpStream:dataReceived:)}
...
wartość domyślna 18:15:46.858460 +0200 apsd incoming message is for proxy slave that is not connected. baseToken destToken publicTokens
wartość domyślna 18:15:46.858734 +0200 apsd timestampForTopic? token
wartość domyślna 18:15:46.858861 +0200 apsd isAppSpecificTokenValid sandbox.push.apple.com
wartość domyślna 18:15:46.869646 +0200 apsd isAppSpecificTokenValid? found cached token NO
usterka 18:15:46.870124 +0200 apsd Received incoming push notification for topic: but for a completely unknown token

where are you trying to send the notifications? and what platform?

https://medium.com/@mariodev_/notificaciones-push-en-react-native-7cedb0d6ebca
this tutorial explains how to configure it

I have the same problem, onRegister and onNotification don't get triggered

@GastonEDiaz have you been able to fix this?

@ponty96 yes!
works only on a physical device!

Sendme your code

I test it on physical device and problem occur.

@kr0000
What is the problem that appears to you?

As i post - "I have the same problem, message is sent by server but iOS device not receive this message.". onNotification method is not called. But i receive logs from APNS " apsd Created power assertion {identifier: APSCourier(tcpStream:dataReceived:)} ..."

@kr0000 call PushNotification in your component?

Local notifications works correctly.

@kr0000 remote your need implement the certificates

On the server side i know.

Sent from my iPhone

On 17 Jul 2018, at 17:17, Gaston Diaz notifications@github.com wrote:

@kr0000 remote your need implement the certificates


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

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

It works without any problem on a genymotion emulator if you install OpenGapps

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

Hi,
I am not using using Firebase in my application because there is no need for any Remote notifications in my application as of now. So I have not configured anything related to Firebase in my app.
Now if I try to use PushNotification.configure() it is throwing an error: "Default FirebaseApp is not initialized in this process. Make sure to call Firebase.initialize.App".
Could some one help me?

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

Was this page helpful?
0 / 5 - 0 ratings