Caman
Caman

Reputation: 27

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoPermissions.Permissions.getAsync')]

I can't get the token to the push notifications and permission doesn't work:

Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoPermissions.Permissions.getAsync')

This is the code:

registerForPushNotificationsAsync = async ()=> {
  const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
  let finalStatus = status;
  if (status !== 'granted') {
    const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
    finalStatus = status;
  }
  if (finalStatus !== 'granted') {
    return;
  }
  let token = await Notifications.getExpoPushTokenAsync();
  console.log(token);
}

Upvotes: 1

Views: 9881

Answers (2)

Atmas
Atmas

Reputation: 2393

I was using the right package, but I blindly wrote my import wrong. The way Rusty had it is still correct, but for others who were doing the right thing but maybe fell into this simple trap, be sure to check this too.

DON'T import like this:

import Permissions from 'expo-permissions';

DO import like this:

import * as Permissions from 'expo-permissions';

...If not you will see the same error message even using the CORRECT package!

Upvotes: 1

Rusty
Rusty

Reputation: 4473

Permissions are moved from 'expo' package to 'expo-permissions'

You'll have to install expo-permissions package first then import it using the following syntax

import * as Permissions from 'expo-permissions';

Upvotes: 9

Related Questions