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.

onNotification not getting called

See original GitHub issue

Bug

I’m trying to use remote silent notifications to wake up my app and run some logic as a result of waking up, but my onNotification is never called. onRegister works fine and I get the token when the app starts, but no other method is called.

I’m using Firebase Python Admin SDK to send the test notifications. Noisy notification are received fine by the phone, when I click on them my App starts but the onNotification code is not called in that case either.

The silent notification im sending in python is:

message = messaging.Message(
        data={
            'foo': 'bar',
        },
        token=TOKEN,
    )

Environment info

react-native info output:

System:
    OS: Linux 5.4 Ubuntu 18.04.5 LTS (Bionic Beaver)
    CPU: (4) x64 Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz
    Memory: 786.65 MB / 15.56 GB
    Shell: 5.4.2 - /usr/bin/zsh
  Binaries:
    Node: 14.15.1 - ~/.nvm/versions/node/v14.15.1/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v14.15.1/bin/npm
    Watchman: 20201120.183554.0 - /usr/local/bin/watchman
  SDKs:
    Android SDK:
      API Levels: 23, 24, 25, 26, 27, 28, 29
      Build Tools: 23.0.1, 23.0.3, 25.0.0, 25.0.2, 25.0.3, 26.0.1, 28.0.3, 29.0.2
      System Images: android-25 | Google Play Intel x86 Atom, android-27 | Google Play Intel x86 Atom, android-29 | Google APIs Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: Not Found
  Languages:
    Java: 1.8.0_212 - /usr/bin/javac
    Python: 2.7.17 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.1 => 17.0.1 
    react-native: ^0.63.4 => 0.63.4 
  npmGlobalPackages:
    *react-native*: Not Found

Library version: “react-native-push-notification”: “^6.1.3”,

My files:

index.android.js

import React, { Component } from 'react';
import { AppRegistry, PermissionsAndroid, Alert } from 'react-native';
import App from './src/main';

import PushNotificationIOS from "@react-native-community/push-notification-ios";
import PushNotification from "react-native-push-notification";

PushNotification.configure({
  // (optional) Called when Token is generated (iOS and Android)
  onRegister: function (token) {
    console.log("TOKEN:", token);
  },

  // (required) Called when a remote is received or opened, or local notification is opened
  onNotification: function (notification) {
    console.log("NOTIFICATION:", notification);

    // process the notification

    // (required) Called when a remote is received or opened, or local notification is opened
    notification.finish(PushNotificationIOS.FetchResult.NoData);
  },

  // (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
  onAction: function (notification) {
    console.log("ACTION:", notification.action);
    console.log("NOTIFICATION:", notification);

    // process the action
  },

  // 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
   * - if you are not using remote notification or do not have Firebase installed, use this:
   *     requestPermissions: Platform.OS === 'ios'
   */
  requestPermissions: true,
});

// do Android specific stuff here
export default class AndroidApp extends Component {
  // Permisos requeridos.
  PERMISSIONS = [
    PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
    PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
    PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE,
    PermissionsAndroid.PERMISSIONS.USE_SIP
  ]

  constructor(props) {
    super(props);
    this.permissionsCheck();
  }

  async permissionsCheck() {
    // Verificamos permisos.
    let pending = [];
    this.PERMISSIONS.forEach(permission => {
      const enabled = PermissionsAndroid.check(permission);
      if (!enabled) {
        console.log('Permission: ' + permission + ' [Denaid]');
        pending.push(permission);
      }
    });

    if (pending.length > 0) {
      // Solicitamos los permisos si aun no los tiene.
      try {
        const permissions = await PermissionsAndroid.requestMultiple(pending);
      } catch (err) {
        Alert.alert('Error', 'Permissions Failed!!');
      }
    }
  }

  render() {
    return <App/>;
  }
}

AppRegistry.registerComponent('myapp', () => AndroidApp);

android/build.gradle


buildscript {
    ...
    dependencies {
        ...
        classpath('com.google.gms:google-services:4.3.3')
        ...
    }
}

android/app/build.gradle

dependencies {
  ...
  implementation 'com.google.firebase:firebase-analytics:18.0.0'
  ...
}

apply plugin: 'com.google.gms.google-services'

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
hitokiri82commented, Dec 21, 2020

I’ve managed to get remote data-only notifications working by adding (on top of everything we’ve discussed so far on the thread) the following to the <application> section in my AndroidManifest.xml:

      <service
        android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
      </service>

By data-only notifications I mean like this example using the python Firebase Admin SDK:

    message = messaging.Message(
        data={
            'foo': 'bar',
        },
        token=TOKEN,
    )
    response = messaging.send(message)
1reaction
hitokiri82commented, Dec 21, 2020

I haven’t found a solution yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

onNotification is not called when a remote notification ... - GitHub
Hi, onNotification is not get called when i press a notification. The doc https://github.com/zo0r/react-native-push-notification#usage says ...
Read more >
OnNotification() function dosen't work when i send ...
1 Answer 1 ... PushNotificationIOS.getInitialNotification().then(notification => { if (!notification) { return; } });. I just implemented and ...
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 >
react-native-push-notification - npm
If not using a built in Android color ( @android:color/{name} ) for the ... or local notification is opened onNotification: function ...
Read more >
Handling Incoming Push Notifications in AWS Amplify - Medium
I have not yet tested how iOS push notifications interact with the push ... onNotification handler function is called when a push notification...
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