10

I Have a simple code that asks for notifications permission that worked in the past, but suddently,It's giving me this error:

"Error: Encountered an exception while calling native method: Exception occurred while executing exported method requestPermissionsAsync on module ExpoNotificationPermissionsModule: String resource ID #0xffffffff"

Code:

    if (isDevice) {
        const { status: existingStatus } = await Notifications.getPermissionsAsync();
        let finalStatus = existingStatus;
        if (existingStatus !== "granted") {
            const { status } = await Notifications.requestPermissionsAsync();
            finalStatus = status;
        }
        if (finalStatus !== "granted") {
            Alert.alert("Falha ao obter permissão para notificações push!", "É necessário permitir o envio de notificações push para o aplicativo funcionar corretamente.");
            return "";
        }
        token = (await Notifications.getExpoPushTokenAsync()).data;
    } else {
        alert("Para gerar o token de notificação você precisa estar em um dispositivo físico!");
    }

4 Answers4

12

On Android 13, app users must opt-in to receive notifications via a permissions prompt automatically triggered by the operating system. This prompt will not appear until at least one notification channel is created. The setNotificationChannelAsync must be called before getDevicePushTokenAsync or getExpoPushTokenAsync to obtain a push token. You can read more about the new notification permission behavior for Android 13 in the official documentation.

async function registerForPushNotificationsAsync() {
  let token;

  if (Platform.OS === 'android') {
    await Notifications.setNotificationChannelAsync('default', {
      name: 'default',
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: '#FF231F7C',
    });
  }

  if (Device.isDevice) {
    const { status: existingStatus } = await Notifications.getPermissionsAsync();
    let finalStatus = existingStatus;
    if (existingStatus !== 'granted') {
      const { status } = await Notifications.requestPermissionsAsync();
      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');
  }

  return token;
}

Clear storage data and Uninstall expo go app. Download it again and app should be prompting permission.

Irfan Muhammad
  • 715
  • 2
  • 8
  • 20
  • This was very helpful and resolved the issue for me. Just a note for anybody else trying this, I didn't need to clear storage data. Just uninstalling and reinstalling the app after adding the `Notifications.setNotificationChannelAsync` code sufficed for me. – Stretch0 Sep 02 '23 at 11:29
7

Same story here, it was working till recently. Didnt even look to part of code responsible for registering for notifications. Since it's not related to codebase look for issue source elswhere. My phone updated to new Os version recently, so i have uninstalled expo and clear all the app data/storage. Also i've updated expo-cli and started with untoutched codebase with the same device. I got prompted for permission for notifications and this part of code went trough. Although secure storage is not working now :-). Anyway that's a starting point for you.

user2634449
  • 114
  • 1
  • 6
0

Irfan Muhammad's solution worked for me,

I also moved the registerForPushNotificationsAsync function to the top of my App.js file before I uninstalled expo go and reinstalled it.

Not sure if moving the function made a difference but it's what I did. The prompt then immediately came up.

Matt T
  • 1
0

For fixing this follow this: expo notification setup

  • Add this if not present
  if (Platform.OS === 'android') {
    await Notifications.setNotificationChannelAsync('default', {
      name: 'default',
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: '#FF231F7C',
    });
  }
  • Don't forget to add the projectId inside getExpoPushTokenAsync
  token = (
      await Notifications.getExpoPushTokenAsync({
        projectId: Constants.expoConfig.extra.eas.projectId,
      })
  • Put the whole code in a try-catch for detecting future issues
sreedeep
  • 1
  • 1