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.

Notifications Received Listener and Notifications Response Listener are not triggered

See original GitHub issue

Summary

expo-notifications subscription functions “addNotificationReceivedListener” and “addNotificationResponseReceivedListener” do not fire when either push or local notifications are received while the app is in the foreground.

In the provided example (example taken from expo-notification doc), the listener never fires the callback function.

Managed or bare workflow? If you have made manual changes inside of the ios/ or android/ directories in your project, the answer is bare!

managed

What platform(s) does this occur on?

iOS

Package versions

package.json dependencies :

“dependencies”: { “@types/react-dom”: “~17.0.11”, “date-fns”: “^2.28.0”, “expo”: “~45.0.0”, “expo-av”: “~11.2.3”, “expo-dev-client”: “~0.9.5”, “expo-notifications”: “~0.15.2”, “expo-status-bar”: “~1.3.0”, “expo-task-manager”: “~10.2.1”, “onesignal-expo-plugin”: “^1.0.2”, “react”: “17.0.2”, “react-dom”: “17.0.2”, “react-native”: “0.68.1”, “react-native-onesignal”: “^4.3.9”, “react-native-web”: “0.17.7” }, “devDependencies”: { “@babel/core”: “^7.12.9”, “@types/react”: “~17.0.21”, “@types/react-native”: “~0.66.13”, “typescript”: “~4.3.5” },

Environment

expo-env-info 1.0.3 environment info: System: OS: macOS 12.0 Shell: 5.8 - /bin/zsh Binaries: Node: 16.15.0 - /opt/homebrew/bin/node Yarn: 1.22.18 - /opt/homebrew/bin/yarn npm: 8.5.5 - /opt/homebrew/bin/npm Watchman: 2022.03.21.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.3 - /opt/homebrew/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.0.1, iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0 IDEs: Xcode: 13.1/13A1030d - /usr/bin/xcodebuild npmPackages: expo: ~45.0.0 => 45.0.1 react: 17.0.2 => 17.0.2 react-dom: 17.0.2 => 17.0.2 react-native: 0.68.1 => 0.68.1 react-native-web: 0.17.7 => 0.17.7 npmGlobalPackages: eas-cli: 0.52.0 expo-cli: 5.4.3 Expo Workflow: managed

Reproducible demo

import { StatusBar } from ‘expo-status-bar’; import { StyleSheet, Text, View, TouchableOpacity, ScrollView } from ‘react-native’; import { Video } from ‘expo-av’; import * as Notifications from ‘expo-notifications’ import { useRef, useState } from ‘react’; import { useEffect } from ‘react’; import registerForPushNotificationsAsync from ‘./registerForPushNotificationsAsync’; import * as TaskManager from ‘expo-task-manager’ import OneSignal from ‘react-native-onesignal’ import {schedulePushNotification} from ‘./scheduleNotification’

Notifications.setNotificationHandler({ handleNotification: async () => ({ shouldShowAlert: true, shouldPlaySound: false, shouldSetBadge: false, }), })

OneSignal.setAppId(APP_ID)

export default function App() {

const [expoPushToken, setExpoPushToken] = useState(“”) const [notification, setNotification] = useState(false) const notificationListener = useRef() const responseListener = useRef()

useEffect(() => { registerForPushNotificationsAsync().then((token) => setExpoPushToken(token)) console.log(‘rub’) Notifications.addNotificationReceivedListener((notification) => { console.log(“notification received”) })

responseListener.current =
  Notifications.addNotificationResponseReceivedListener((response) => {
    console.log(response)
  })

return () => {
  Notifications.removeNotificationSubscription(notificationListener.current)
  Notifications.removeNotificationSubscription(responseListener.current)
}

}, [])

function scheduleLocalNotification() { Notifications.scheduleNotificationAsync({ content: { title: “local notification” }, trigger: { seconds: 1 }, }) }

return ( <ScrollView > <View style={styles.container}> <View style={{flex: 1, padding: 20, flexDirection : ‘row’}}> <TouchableOpacity onPress={scheduleLocalNotification}> <Text> Set Local Notification </Text> </TouchableOpacity> <TouchableOpacity onPress={() => scheduleAlarmNotification()}> <Text> Set OneSignal Notification </Text> </TouchableOpacity> </View> </View> </ScrollView> ); }

Stacktrace (if a crash is involved)

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
xD3CODERcommented, May 25, 2022

Got same issue, seems to be a conflict between expo-notificationsand react-native-onesignal. Everything works again if I remove the OneSignal service extension from my app. If both are installed, events are not triggered on expo-notifications side, and custom actions defined by expo-notifications don’t work either. Everything works well on Android

0reactions
mrzmyrcommented, Dec 8, 2022

Same here, seems like react-native-onesignal was causing some issues preventing setNotificationHandler from triggering.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expo push notifications listener not firing - Stack Overflow
I'm implementing push notifications in an app, but I need to handle the notification to navigate to a specific screen.
Read more >
Receiving Notifications - Expo Documentation
Like most things with Expo, handling notifications is simple and straightforward across all platforms. All you need to do is add a listener...
Read more >
Advanced: Notifications: SUCCESSFUL CODE - React Native
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed) ......
Read more >
Expo Push Notification Foreground Not Working : r/reactnative
Foreground does not work. ... addNotificationReceivedListener(notification ... alert('Failed to get push token for push notification!
Read more >
notifications.onClosed - Mozilla - MDN Web Docs
Check whether listener is registered for this event. Returns true if it is listening, false otherwise. addListener syntax. Parameters.
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