【问题标题】:LocalNotification not working [IOS] - PushNotificationIOSLocalNotification 不工作 [IOS] - PushNotificationIOS
【发布时间】:2020-02-20 07:39:47
【问题描述】:

我正在尝试使用 @react-native-community/push-notification-ios 包在 IOS 中实现本地通知。

我正确地遵循了所有文档。尽管如此,LocalNotification 仍然无法正常工作。

这是我的环境配置: -react-native0.61.4 -@react-native-community/push-notification-ios --save1.0.5

我做了以下事情,

  1. npm i @react-native-community/push-notification-ios --save
  2. cd ios && pod install
  3. 根据here 的描述更新了AppDelegate.m
  4. 构建:react-native run-ios --device "iPhone X"
  5. 然后像这样在我的js中调用函数,
import PushNotificationIOS from "@react-native-community/push-notification-ios";
.
.
.
componentDidMount(){
  PushNotificationIOS.addEventListener('localNotification', this._onNotification);

  PushNotificationIOS.requestPermissions();
  PushNotificationIOS.presentLocalNotification({
    alertBody: 'Test Notification'
  });
}

_onNotification(notification) {
  console.log(notification._alert);
}
.
.
.
  • 顺便说一句,它会在首次打开应用程序时请求权限,而且我收到了console.log 的通知,但没有收到任何本地通知。

【问题讨论】:

    标签: ios xcode react-native push-notification localnotification


    【解决方案1】:

    好吧,当应用程序在前台运行时,您将无法看到通知。你可以调用本地日程功能,快速隐藏应用到后台,然后你会看到通知。

    源代码:-

    PushNotificationIOS.localNotificationSchedule({
          message: "Local push notification",   //mandatory
          number: 1,
          date: new Date(Date.now() + (5 * 1000)) // Schedule in 5 secs
        });
    

    【讨论】:

    • 哦,所以在 IOS 中无法在应用程序运行时收到通知。那么提供PushNotificationIOS.presentLocalNotification这类功能是什么意思呢?
    猜你喜欢
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2013-06-02
    相关资源
    最近更新 更多