I can't get the Token for the notifications

Help please!

Problem 1: If I don’t allow notifications on Expo, I never get the Notifications Permissions Prompt.

Problem 2: If I do allow them and use the code in the Expo docs (https://docs.expo.io/versions/latest/guides/push-notifications), then I get a warning that says “Possible Unhandled Promise Rejection”, and the token never loads.

Problem 3: Once I remove the await in the let token = await Notifications.getExpoPushTokenAsync(); I get a Promise that says:

Promise { [16:09:25] "_40": 0, [16:09:25] "_55": null, [16:09:25] "_65": 0, [16:09:25] "_72": null, [16:09:25] }}

This wasn’t what I was expecting for, since I was expecting something like a

ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]

Here is my code:


const PUSH_ENDPOINT = 'https://your-server.com/users/push-token';

export async function registerForPushNotificationsAsync() {
  const { status: existingStatus } = await Permissions.getAsync(
    Permissions.NOTIFICATIONS
  );
  let finalStatus = existingStatus;

  // only ask if permissions have not already been determined, because
  // iOS won't necessarily prompt the user a second time.
  if (existingStatus !== 'granted') {
    // Android remote notification permissions are granted during the app
    // install, so this will only ask on iOS
    const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
    finalStatus = status;
  }

  // Stop here if the user did not grant permissions
  if (finalStatus !== 'granted') {
    return;
  }

  // Get the token that uniquely identifies this device
  let token = await Notifications.getExpoPushTokenAsync();
  console.log("im the token: ", token);
}

Hey @wilfredo789,

Please put all your code into a Snack http://snack.expo.io (including your import statements) so I can see if you have any implementation errors. Also, please refrain from creating multiple posts for the same issue. Doing so won’t result in getting quicker support and it creates more noise and mess on the forums. I’m going to close your other post to avoid confusion.

Cheers,

Adam

Here is the snack: notifications wilfredo - Snack

On Android: I dont get the “Allow Notifications” prompt. Then I get the alert but nothing inside.

On my iPhone5 I dont get the prompt neither the alert.

On an iPhone s6 I get the prompt. Then after a long wait I got the alert but no token inside.

hello?

are you there Adam? We need help with this

Hey @wilfredo789,

Your implementation of the Alert api is wrong. If you just console.log it you’ll see that you are getting the token as expected, but if you want to see it in the alert you can run:

alert(`I'm the token: ${token}`)

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