首页 > 解决方案 > React Native Expo - 安卓推送通知

问题描述

我正在尝试使用 expo 推送通知工具向 android 设备发送推送通知,(它适用于 iOS)但它不起作用,我添加了一个带有单独的 android 应用程序 google-service.json 的 firebase 项目,并运行了 expo push:android:upload --api-key 。有什么遗漏吗?

我使用托管工作流 SDK 39

这是我使用过的代码,它适用于 iOS

获取博览会推送通知代码

async function registerForPushNotificationsAsync() {
  let token;
  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') {
      
      return;
    }
    token = (await Notifications.getExpoPushTokenAsync()).data;
    let uid;
    firebase.auth().signInAnonymously()
      .then(() => {
        uid = firebase.auth().currentUser.uid;
      })
      .then(() => {
        db.collection('users').doc(uid).get()
            .then(function(doc) {
                if (doc.exists) {
                    db.collection('users').doc(uid).update({
                      expoPushToken: token,
                      'last use date': new Date().toISOString().slice(0,10),
                      'region': Localization.region
                    })
                } else {
                    db.collection('users').doc(uid).set({
                      expoPushToken: token,
                      'last use date': new Date().toISOString().slice(0,10),
                      'region': Localization.region
                    })
                }
            })
      })
      .catch(() => console.log('ERROR'))

  } 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',
    });
  }

  return token;
}

应用程序.json

"android": {
      "useNextNotificationsApi": true,
      "googleServicesFile": "./google-services.json",
      "adaptiveIcon": {
        "foregroundImage": "./assets/adaptive-icon.png",
        "backgroundColor": "#FFFFFF"
      },
      "package": "com.NAME.NAME",
      "versionCode": 3
    },

标签: androidfirebasereact-nativeexpoexpo-notifications

解决方案


推荐阅读