简体   繁体   中英

Expo push notifications not working on Android standalone app

I'm working on an expo app and I've added expo push notifications and that's working for iOS but not on android. The problem is caused by getExpoPushTokenAsync() because it doesn't resolve.

I'm working with Expo version 40.0.0 and using the following code to get the notification token:

export async function registerForPushNotificationsAsync(t) {
  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(t('no_access_push_notifications'));
      return;
    }
    const token = await Notifications.getExpoPushTokenAsync();
    return token;

    // this.setState({ expoPushToken: token });
  } else {
    console.log("No physical device");
  }

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

I've created a new project in Firebase according to the Expo docs for Android and copied the google-services.json to the root of my app and linked it in my app.json

{
  "project_info": {
    "project_number": "xxxxxxxxx",
    "project_id": "xxxxxxxxx",
    "storage_bucket": "xxxxxxxxx"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "xxxxxxxxx",
        "android_client_info": {
          "package_name": "xxxxxxxxx"
        }
      },
      "oauth_client": [
        {
          "client_id": "xxxxxxxxx",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "xxxxxxxxx"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "xxxxxxxxx",
              "client_type": 3
            }
          ]
        }
      }
    }
  ],
  "configuration_version": "1"
}

My app.json contains the following code:

"android": {
      "package": "xxxxx",
      "versionCode": 4050,
      "googleServicesFile": "./google-services.json",
      "config": {
        "googleSignIn": {
          "apiKey": "xxxxx"
        }
      },
      "useNextNotificationsApi": true
    },

Any thoughts on this?

Notifications.getExpoPushTokenAsync() returns an object, not a token.

Returns a Promise that resolves to an object with the following fields:

    type (string) -- Always expo.
    data (string) -- The push token as a string.

You should use somthing like this:

await Notifications.getExpoPushTokenAsync().then(({data}) => this.setState({expoPushToken: data}) )
console.log(this.state.expoPushToken);

And very important to set up correct "API restrictions" in the Google Cloud Platform API Credentials console. Set type Android for Android Token and add SHA1 fingerprint. You need to fetch expo android keystore and send it to keytool from Java to see fingerprint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM