Hi everyone,
We have a managed app in production on iOS and Android. Notifications are perfectly working but we don’t know how to deal with the native iOS setting to disable the notifications on the app.
The notifications are sent by our backend server using the Expo’s Push API and the response (action from the user) are handled on the client app.
If a user “cuts” the notifications from his iOS settings, he won’t receive notifications anymore (because Apple will cut them) but we’ll get error from Apple telling us stop sending notifications for this specific Push Token.
The question is, what is the correct way to know when this setting is turned on/off ?
Actually, we’re using the AppState feature to call the Notifications.getPermissionsAsync()
function to know if the status is granted or not. It works fine when the user opens the app after changing the settings but what if he cuts the notification and never opens the app again ? We will never know if we should still send notifications or not.
Here our source code :
const sendNotificationDeviceStatus = async (): Promise<void> => {
const settings = await Notifications.getPermissionsAsync();
const isAuthorized =
settings.granted ||
settings.ios?.status === Notifications.IosAuthorizationStatus.PROVISIONAL ||
settings.ios?.status === Notifications.IosAuthorizationStatus.AUTHORIZED;
sendNotificationStatusToTheBackend(isAuthorized)
};
const onChangeState = useCallback((nextAppState: string): void => {
const checkUpdate = async (): Promise<void> => {
try {
const { isAvailable } = await Updates.checkForUpdateAsync();
if (isAvailable) setUpdateRequired(true);
} catch (e) {
setUpdateRequired(false);
}
};
if (nextAppState === "active") {
sendNotificationDeviceStatus();
checkUpdate();
}
}, []);
useEffect(() => {
AppState.addEventListener("change", onChangeState);
sendNotificationDeviceStatus();
responseListener.current = Notifications.addNotificationResponseReceivedListener((response) => {
const type = response?.notification?.request?.content?.data?.type;
switch (type) {
case notificationsEnum.SENSOR:
navigate("MainFlow", { screen: "RealTimeScreen" });
return notificationsEnum.SENSOR;
default:
return response;
}
});
return () => {
Notifications.removeNotificationSubscription(responseListener.current);
};
}, [onChangeState]);
Thanks for your time and your help
SDK Version: 42
Platform: iOS