Permissions.getAsync() returns undefined

Please provide the following:

  1. SDK Version: 39.0.2
  2. Platforms(Android/iOS/web/all): Android

Hey guys. I wanted to implement Push Notifications in my app. So the first thing to do initially is grant permissions. So I am asking for permissions with my useeffect hook by calling this function. Also I am using typescript.

async function registerForPushNotificationsAsync() {
        let token;
        if (Constants.isDevice) {
            const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
            let finalStatus = existingStatus;
            if (existingStatus !== "granted") {
                const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
                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');
        }

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

        return token;
    }

So far so good. The imports on the top are like this:

import  * as Permissions from "expo-permissions";

import {Notifications} from "expo";

When I try to run the app on my android device (no emulator) I am getting the unhandled Promise rejection warning because Permissions.getAsync() returns undefined.

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoNotifications.default.getExpoPushTokenAsync')]

What I’ve done:

  • purged node_modules and installed new
  • I switched from import Permissions from "expo-permissions" to import * as Permissions from "expo-permissions"
  • I cleared cache with expo r -c

Edit: What I am now seeing, that the error-message from above is not the original one, so I think maybe there is also something wrong with the notification import or something. I dont know.

Hey @lukas.germerott, it looks like you are importing Notifications from the expo package which is incorrect for sdk39. You’ll want to install the expo-notifications package and import from there. See: https://docs.expo.io/versions/v39.0.0/sdk/notifications/#api

Cheers,
Adam

Hey @adamjnav thank you for your reply.

Yeah exactly. I have expo-notifications installed but I tried importing it wrong before. I tried import Notifications from "expo-notifications". But now after changing it to import * as Notifications from "expo-notifications" it works.

1 Like

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