【问题标题】:IOS Notification Permission alert does not showIOS 通知权限警报不显示
【发布时间】:2020-11-11 09:31:03
【问题描述】:

SDK 版本:39.0.0 平台(Android/iOS/web/all):全部 在生产环境中加载我的应用时,我没有收到接受或拒绝通知权限提醒。

我已尝试清除证书和密钥,并允许 expo 从头开始​​添加所有内容,但仍然没有运气。我开始认为这可能是我的代码导致警报没有被触发的原因。

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"
      });
    }
  };

【问题讨论】:

    标签: ios react-native apple-push-notifications expo


    【解决方案1】:

    从 "expo-permissions" 导入 { Permissions };

    应该是

    import * as Permissions from 'expo-permissions';

    有时我们都会犯一些简单的错误。

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 2014-11-18
      • 2019-04-17
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2017-07-12
      • 2018-03-20
      • 2017-03-16
      相关资源
      最近更新 更多