I am trying to access push notification data with killed app. I have been reading the docs and implemented the steps that are provided but still i don’t get any data, it’s like the push notification is not detected.
I am using: “expo”: “^42.0.0”, “expo-notifications”: “~0.12.3”,
import { enableScreens } from 'react-native-screens'
import { NavigationContainer } from '@react-navigation/native';
import CarNavigator from './src/navigation/CarNavigator'
import { Alert } from 'react-native';
import React, { useState, useEffect, useRef } from 'react'
import * as Notifications from 'expo-notifications'
import Constants from 'expo-constants';
enableScreens();
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
export default function App() {
const [expoPushToken, setExpoPushToken] = useState('');
const [notification, setNotification] = useState(false);
const notificationListener = useRef();
const responseListener = useRef();
const registerForPushNotificationsAsync = async () => {
let token;
if (Constants.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== 'granted') {
console.log('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
} else {
console.log('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
return token;
}
useEffect(() => {
registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
});
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
Alert.alert('IN');
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
/* END */
return (
<NavigationContainer>
<CarNavigator />
</NavigationContainer>
)
}
As per the documentation this part handles push notification data while app is killed as stated here: Notifications - Expo Documentation
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
Alert.alert('IN');
});
When i send a push notification and start the app using that notification the Alert is not triggered, it’s like the push notification is not detected.
I am testing this on android with a standalone apk build.
Many thanks, Trix