Notifications Received Listener and Notifications Response Listener are not triggered
See original GitHub issueSummary
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:
- Created a year ago
- Comments:6
Got same issue, seems to be a conflict between
expo-notifications
andreact-native-onesignal
. Everything works again if I remove the OneSignal service extension from my app. If both are installed, events are not triggered onexpo-notifications
side, and custom actions defined byexpo-notifications
don’t work either. Everything works well on AndroidSame here, seems like
react-native-onesignal
was causing some issues preventingsetNotificationHandler
from triggering.