IOS Notification Permission alert does not show

    1. SDK Version: 39.0.0
  1. Platforms(Android/iOS/web/all): All

I am not getting the accept or decline notifications permissions alert when loading my app in production.

I have tried clearing certificates and keys and allowing expo to add everything from a clean slate, but still no luck. I am starting to think maybe it’s my code which is the reason why the alert doesn’t get fired.

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

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: false
  })
});

export default class LoginScreen extends React.Component {
  state = {
    email: "",
    password: "",
    notification: {},
    errorMessage: null
  };

  async componentDidMount() {
    this.registerForPushNotificationsAsync();
    //Notifications.addNotificationReceivedListener(this._handleNotification);
    Notifications2.addListener(data => {
      this.setState({ notification: data });
    });

    Notifications.addNotificationResponseReceivedListener(
      this._handleNotificationResponse
    );
  }

  _handleNotification = notification => {
    this.setState({ notification: notification });
  };

  _handleNotificationResponse = response => {
    console.log(response);
  };

  handleLogin = async () => {
    try {
      const { email, password } = this.state;

      const expoPushToken = await Notifications.getExpoPushTokenAsync();

      console.log(expoPushToken);

      const userinfo = await firebase
        .auth()
        .signInWithEmailAndPassword(email, password);
      console.log("user ID ", userinfo.user.uid);

      await firebase
        .firestore()
        .collection("users")
        .doc(userinfo.user.uid.toString())
        .update({
          expo_token: expoPushToken["data"]
        })
        .then(function() {
          console.log("token successfully updated!");
        })
        .catch(function(error) {
          // The document probably doesn't exist.
          console.error("Error updating document: ", error);
        });
    } catch (error) {
      console.log("=======Error in login", error);
      this.setState({ errorMessage: error.message });
    }
  };


  registerForPushNotificationsAsync = 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") {
        alert("Failed to get push token for push notification!");
        return;
      }
      const token = await Notifications.getExpoPushTokenAsync();
      console.log(token);
      //this.setState({ expoPushToken: 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"
      });
    }
  };

Have you ever been able to see it? One potential gotcha is that Apple doesn’t let you just keep asking the user for permission - they only let you do it the first time. So if you haven’t, you’ll probably want to straight up delete your app and get it reinstalled and see if that makes a difference.

Also, do you know if anything properly loads? I’m surprised your code snippet doesn’t have a render function anywhere (but maybe you removed it since it wasn’t pertinent?).

No, it’s never shown in either development or production, it went over my head because I could receive notifications in development. Yes removed render as didn’t think it was necessary to show.

Yes I know about Apple only allowing the permission once. I have tried on three different phones and also reinstalled app as well as clear certificates and upload new ones and submitted new builds.

Ahh, strange. I’m all out of guesses then. If I remember correctly, I was able to get push tokens without having the proper certificates (though the certificate is needed to send notifications). Best luck figuring it out.

I figured it out wasn’t anything to do with notifications. I went back to development looked at logs and had a warning [evaluating ‘_expo.Permission.askAsync’)]

I changed

import { Permissions } from “expo-permissions”;

to

import * as Permissions from ‘expo-permissions’;

did a quick OTA and the alert popped up, tested a notification and it worked!

1 Like

Glad you got things sorted out, @tomm. FYI: You can handle permissions within the Notifications module as well, rather than having to import the Permissions module. https://docs.expo.io/versions/v39.0.0/sdk/notifications/#fetching-information-about-notifications-related-permissions

This is true for most other modules that require permissions as well.

Cheers,
Adam

1 Like