Adrian Bartholomew
Adrian Bartholomew

Reputation: 2632

Push Notifications don't work after deploying Expo app to Apple store's TestFlight

Push Notifications works perfectly side loaded to my iOS phone. I can fetch the token and successfully save it to and recall it from my Google Firestore db. Recalling it to send out notifications also work as expected. If I'm not in the App, I get a notification. If I am in the app, my Notification Listener works well.

After building to iOS and deploying the stand alone app to TestFlight on Apple's servers, Push Notifications no longer work.

This is my build.

Adrians-MBP:xxxxx abarthol$ expo build:ios -c
Checking if there is a build in progress...

Removed existing credentials from expo servers
Please enter your Apple Developer Program account credentials. These
credentials are needed to manage certificates, keys and provisioning profiles
in your Apple Developer account.
The password is only used to authenticate with Apple and never stored.
? Apple ID: xxxxx
? Password (for xxxxx): [hidden]
Trying to authenticate with Apple Developer Portal...
Authenticated with Apple Developer Portal successfully!
Only 1 team associated with your account, using Apple Team with ID: xxxxx
We do not have some credentials for you: Apple Distribution Certificate, Apple Push Notifications service key, Apple Provisioning Profile
? How would you like to upload your credentials? Expo handles all credentials, y
ou can still provide overrides
? Will you provide your own Apple Distribution Certificate? Let Expo handle the 
process
✔ Didn't find any previously uploaded Apple Distribution Certificate
? Will you provide your own Apple Push Notifications service key? I want to uplo
ad my own file
Please provide your Apple Push Notifications service key:
? Path to P8 file: ~/Sites/certs/aps.cer
? Key ID: xxxxx
✔ App ID found on Apple Developer Portal.
We're going to generate:
- Apple Distribution Certificate
- Apple Provisioning Profile
✔ Generated Apple Distribution Certificate
✔ Generated Apple Provisioning Profile
Encrypted credentials and saved to the Expo servers
Publishing to channel 'default'...
Building iOS bundle
Building Android bundle
Analyzing assets
Uploading assets
No assets changed, skipped.
Processing asset bundle patterns:
- ~/Sites/Personal/xxxxx/**/*
Uploading JavaScript bundles
Published
Your URL is

https://exp.host/@xxxxx/xxxxx

Checking if this build already exists...

Build started, it may take a few minutes to complete.
You can check the queue length at https://expo.io/turtle-status

You can monitor the build at

 https://expo.io/builds/xxxxx

Waiting for build to complete. You can press Ctrl+C to exit.
✔ Build finished.
Successfully built standalone app: https://expo.io/artifacts/xxxxx

Here is my component:

  componentDidMount() {
    this.registerForPushNotifications();
  }

  componentWillUnmount() {
    if (!this.subscription) return;
    this.subscription.remove();
  }

  registerForPushNotifications = async () => {
    const PNToken = await this.props.MainStore.getLocal("PNToken");
    if (!PNToken) {
      try {
        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") {
            throw "Notification permission not granted";
          }
          const token = await Notifications.getExpoPushTokenAsync();
          this.props.MainStore.setPNToken(token);
          this.subscription = Notifications.addListener(
            this.handleNotification
          );
        }
      } catch (err) {
        console.warn("Permissions check error: ", err);
      }
    } else {
      this.props.MainStore.setPNToken(PNToken);
      this.subscription = Notifications.addListener(this.handleNotification);
    }
  };

  handleNotification = notification => {
    const store = this.props.NotificationStore;
    const sortedNotifications = sortMessages([
      ...store.state.notifications,
      { ...notification, read: false }
    ]);
    store.setState({
      notifications: sortedNotifications
    });
  };

Upvotes: 1

Views: 4303

Answers (3)

sambha
sambha

Reputation: 1353

In addition to having the correct "Apple Distribution Certificate", "Apple Provisioning File" & "Apple Push Key" at expo.io, you also have to have the correct device token.

First, find experienceId of your app by executing expo credentials:manager It is shown as Experience: @user/slug, bundle identifier: com.xxx.xxx

Then get your token as below

const token = (
   await Notifications.getExpoPushTokenAsync({
   experienceId: '@user/slug',  // <-- Experience shown above
   })
).data;
console.log('EXPO TOKEN: ', token); // Store this in the backend

Put this device token in the expo test tool (https://expo.io/notifications) and see if you get the notification.

Upvotes: 1

user1760527
user1760527

Reputation: 1164

I had a lot of frustration with push finally solved the issue. 1) Enable Push notifications in Developer portal > Certificates, Identifiers & Profiles > Identifier (of your app) 2) Create your APNS key file as described here https://fluffy.es/p8-push-notification/ 3) Delete existing provisioning profile 4) expo build:ios --clear-credentials, upload your P8 file to expo

Upvotes: 3

Alex Catchpole
Alex Catchpole

Reputation: 7336

From the look of it you're providing the old APNS cert to Expo when it's asking for the new P8 format.

You should be able to generate a new P8 file from the Apple member center.

Upvotes: 1

Related Questions