Please provide the following:
Expo CLI 4.7.3
- SDK Version:
SDKs:
iOS SDK:
Platforms: iOS 14.5, DriverKit 20.4, macOS 11.3, tvOS 14.5, watchOS 7.4
npmPackages:
expo: ^41.0.1 => 41.0.1
react: 17.0.2 => 17.0.2
react-native: 0.64.1 => 0.64.1 - Platforms(Android/iOS/web/all): iOS
- Add the appropriate “Tag” based on what Expo library you have a question on.
Hello everyone,
How can I get the notifications to be shown when I´m outside the app (iOS) but it´s been difficult to achieve it. I´m using a react hook and I don’t know what I’m missing:
import Constants from 'expo-constants';
import React from 'react';
import {Platform} from 'react-native';
import * as Notifications from 'expo-notifications';
if (Platform.OS !== 'android') {
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
}
const useLocaliOSNotificacion = () => {
const [expoPushToken, setExpoPushToken] = React.useState('');
const [notification, setNotification] = React.useState(false);
const notificationListener = React.useRef();
const responseListener = React.useRef();
const navigationiOS = React.useRef(null);
React.useEffect(() => {
if (Platform.OS !== 'android') {
registerForPushNotificationsAsync().then((token) =>
setExpoPushToken(token),
);
notificationListener.current =
Notifications.addNotificationReceivedListener((notification) => {
setNotification(notification);
});
responseListener.current =
Notifications.addNotificationResponseReceivedListener((response) => {
console.log(response);
});
return () => {
Notifications.removeNotificationSubscription(
notificationListener.current,
);
Notifications.removeNotificationSubscription(responseListener.current);
};
}
}, []);
React.useEffect(() => {
console.log('notification.data', notification?.data);
}, [notification]);
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({
ios: {
allowAlert: true,
allowBadge: true,
allowSound: true,
allowDisplayInCarPlay: true,
allowCriticalAlerts: true,
provideAppNotificationSettings: true,
allowProvisional: true,
allowAnnouncements: true,
},
});
finalStatus = status;
}
if (finalStatus !== 'granted') {
alert('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(token);
} else {
alert('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;
};
const schedulePushNotification = async (fnNavigation, msg, usrinfo) => {
if (navigationiOS.current == null) {
navigationiOS.current = fnNavigation;
}
await Notifications.scheduleNotificationAsync({
content: {
title: usrinfo?.payload.placeParams.clinombre,
body: msg,
data: usrinfo,
ios: {sound: true},
sound: 'default',
},
trigger: {seconds: 2},
});
};
const fn = Platform.OS !== 'android' ? schedulePushNotification : () => {};
return [{fn}];
};
export default useLocaliOSNotificacion;
Thank you for your suggestions and help.