世博会通知 - 推送门票/收据状态正常,但未收到通知

问题描述 投票:0回答:1

我正在使用 Expo Managed Workflow 和“expo-notifications”模块向我的应用程序的用户发送推送通知。我已经在我的应用程序中根据文档创建了最小的复制品,并且在接收推送通知时遇到了问题。预定的本地通知工作正常并按预期显示。我还提取了我的 FCM 推送令牌以直接通过 FCM 发送通知,这也按预期工作。

我添加了日志记录以显示推送单/收据状态,并且两者都标记为“正常”,所以我不确定如何继续。

import { useState, useEffect, useRef } from 'react';
import { Text, View, Button, Platform } from 'react-native';
import * as Device from 'expo-device';
import * as Notifications from 'expo-notifications';
import Constants from 'expo-constants';

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

export default function App() {
  const [expoPushToken, setExpoPushToken] = useState('');
  const [notification, setNotification] = useState(false);
  const notificationListener = useRef();
  const responseListener = useRef();

  useEffect(() => {
    registerForPushNotificationsAsync().then(token => setExpoPushToken(token.data));

    notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
      setNotification(notification);
    });

    responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
      console.log(response);
    });

    return () => {
      Notifications.removeNotificationSubscription(notificationListener.current);
      Notifications.removeNotificationSubscription(responseListener.current);
    };
  }, []);

  return (
    <View
      style={{
        flex: 1,
        alignItems: 'center',
        justifyContent: 'space-around',
      }}>
      <Text>Your expo push token: {expoPushToken}</Text>
      <View style={{ alignItems: 'center', justifyContent: 'center' }}>
        <Text>Title: {notification && notification.request.content.title} </Text>
        <Text>Body: {notification && notification.request.content.body}</Text>
        <Text>Data: {notification && JSON.stringify(notification.request.content.data)}</Text>
      </View>
      <Button
        title="Press to schedule a notification"
        onPress={async () => {
          await scheduleNotification(); 
        }}
      />
      <Button
        title="Press to send a push notification"
        onPress={async () => {
          await sendPushNotification(expoPushToken);
        }}
      />
    </View>
  );
}

async function scheduleNotification() {
  console.log("pressed")
  await Notifications.scheduleNotificationAsync({
    content: {
      title: "test",  
      body: 'test3',
      data: {},
    },
    trigger: null
  });
}

async function sendPushNotification(expoPushToken) {
  const message = {
    to: expoPushToken,
    sound: 'default',
    title: 'Original Title',
    body: 'And here is the body!',
    data: { someData: 'goes here' },
  };

  const response = await fetch('https://exp.host/--/api/v2/push/send', {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Accept-encoding': 'gzip, deflate',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(message),
  })

  const responseData = await response.json()
  console.log("Push Ticket : ", responseData.data)
  fetchReceipt(responseData.data.id)
}

async function fetchReceipt(id) {
  ids = new Array(id)
  const message = {
    ids: ids
  }

  const response = await fetch('https://exp.host/--/api/v2/push/getReceipts', {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Accept-encoding': 'gzip, deflate',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(message),
  });

  const responseData = await response.json();
  console.log("Reciept : ", responseData.data)
}

async function registerForPushNotificationsAsync() {
  let token;

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

  if (Device.isDevice) {
    const { status: existingStatus } = await Notifications.getPermissionsAsync();
    let finalStatus = existingStatus;
    if (existingStatus !== 'granted') {
      const { status } = await Notifications.requestPermissionsAsync();
      finalStatus = status;
    }
    if (finalStatus !== 'granted') {
      alert('Failed to get push token for push notification!');
      return;
    }
    // Learn more about projectId:
    // https://docs.expo.dev/push-notifications/push-notifications-setup/#configure-projectid
    token = await Notifications.getExpoPushTokenAsync({
      projectId: Constants.expoConfig.extra.eas.projectId,
    });
    console.log(token);
    const firebaseToken = (await Notifications.getDevicePushTokenAsync()).data;
    console.log("FCM Token : ", firebaseToken)
  } else {
    alert('Must use physical device for Push Notifications');
  }

  return token;
}

日志输出示例:

Push Ticket :  Object {
  "id": "2fbfdd91-53a0-4199-ac32-b53131d72933",
  "status": "ok",
}
Reciept :  Object {
  "2fbfdd91-53a0-4199-ac32-b53131d72933": Object {
    "status": "ok",
  },
}

我知道博览会文档说

Even if a receipt's status says ok, this doesn't guarantee that the device has received the message; "ok" in a push receipt means that the Android or iOS push notification service successfully received the notification.
但是,我能够直接从 FCM 仪表板发送的事实表明我与我的设备通信应该没有问题。

作为参考,我正在真实的 Android 设备上运行 EAS 开发版本。

push-notification expo firebase-cloud-messaging expo-notifications
1个回答
0
投票

你解决这个问题了吗?如果是这样,怎么办?我对世博会通知也有同样的问题。博览会通知工具和端点 (https://api.expo.dev/v2/push/send) 返回 200,但在仪表板上 (https://expo.dev/accounts/[org]/projects/[项目]/push-notifications)每个通知都被标记为失败,并且没有日志可以让我检查我的配置有什么问题(尽管我遵循了官方文档)

© www.soinside.com 2019 - 2024. All rights reserved.