Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving error while reqeusting notification permission on expo app

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!");
    }
like image 349
Lucas Gardini Dias Avatar asked Mar 22 '26 08:03

Lucas Gardini Dias


1 Answers

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.

like image 67
Irfan Muhammad Avatar answered Mar 24 '26 22:03

Irfan Muhammad