1
votes

Firstly i will be really appreciated for your helps. I'am trying to implement expo push notification using expo-bare workflow. When i run app, it gives me error like this so i could not get token. error [TypeError: null is not an object (evaluating '_ExponentNotifications.default.getExponentPushTokenAsync')]

Here is my code:

import { Notifications } from "expo";
import * as Permissions from "expo-permissions";
import Constants from "expo-constants";

export const getToken = async () => {
  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") {
      console.log("Failed to get push token for push notification!");
      return;
    }
    token = await Notifications.getExpoPushTokenAsync();
  }
  else {
    console.log("Must use physical device for Push Notifications");
  }
  return token;
};
2
Bare workflow uses expo-notifications package - github.com/expo/expo/tree/master/packages/expo-notifications - daybreaker
I followed this instruction that you commented sir. Maybe i may try your way you share code below and i will inform you back. Thank you for your help sir. - Huseyin Akcicek

2 Answers

0
votes

To expand on my comment some, I had a similar setup as yours when I was on Expo managed, but I ejected. According to their docs, the bare workflow is a little different, and so mine is set up like this:

// at the top:

import * as Notifications from 'expo-notifications';

// --------

// inside the code:

let settings = false

settings = Notifications.getPermissionsAsync()

if (!settings.granted) {
  settings = Notifications.requestPermissionsAsync()
}

if (settings.status === 1 || settings.status === 'granted') {
  const experienceId = '@proj/example' // (see docs on using expo credentials:manager)
  const token = Notifications.getExpoPushTokenAsync({ experienceId })

  const resp = api.sendNotificationToken({ token: token.data })
}

0
votes

You need to install the expo notifications package:

expo install expo-notifications also don't forget to cd into the ios folder and run pod install to properly link the package.

Next, you need to change the way you import and use Notifications like:

import * as Notifications from 'expo-notifications';

Then to get expo token you do something like:

Notifications.getExpoPushTokenAsync({experienceId:'@your_username/your_app_slug'})

which would resolve to a promise containing your expo token in an object format like:

{"data": "ExponentPushToken[ID]", "type": "expo"}

NB: Most method support in the Notifications from expo is different from the one in expo-notifications. You should check here for more details.