Handle Notifications when Silent Mode (iOS)

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 :slight_smile:

SDK Version: 42
Platform: iOS

You’ll receive an error back from the Push API, right? So when you receive that error, simply remove the associated push token

Completely agreed, we thought about it but what if the opposite case happened ?
He enables the notifications from iOS settings and doesn’t open the app. We already deleted the push token because we received an error on the first try so we won’t be able to send him a notification again before he opens the app in the future ?

Yeah, that’s correct as well- your user would need to reopen the app for you to check the permissions again

Okay so basically, to sum everything up :

  • If the user cuts the notification and never opens the app again, we can just delete his push token when we’ll receive an error from Apple
  • If the user enables the notification and also, never opens the app again, we will have to wait for him to open it again to send him new new push notify.

That was my initial question, if I didn’t miss anything => thanks a lot :slight_smile:

Yep, that’s correct!

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.