【问题标题】:Handling automatically notification data on iOS when app is in foreground state当应用程序处于前台状态时,在 iOS 上自动处理通知数据
【发布时间】:2021-09-06 03:24:28
【问题描述】:

使用react-native-push-notification-ios,我希望通知到达,而不是显示,并将通知数据传递给 React Native 层到 onNotification 函数。

为此,我从 userNotificationCenter: willPresentNotification: withCompletionHandler 函数中删除了 UNNotificationPresentationOptionAlert。但是,我不确定如何处理通知对象,以便自动传递给 React Native。

我该怎么做?

下面显示的代码是我目前正在使用的。它在通知到达时起作用,但不会传递给 React Native。

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
      willPresentNotification:(UNNotification *)notification
        withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  NSLog(@"???? Notification received!");
  completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge);
}

更新 1

我找到了一种直接处理通知的方法,但是它的内容似乎是空的:

上面我调整的代码:

-(void)userNotificationCenter:(UNUserNotificationCenter *)center
      willPresentNotification:(UNNotification *)notification
        withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  NSLog(@"???? Notification received in foreground!");
  completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge);
  NSDictionary *dict = @{ @"key" : @"asd", @"key2" : @"qwe"}; // This is a test, this object is correctly passed to React Native
  NSLog(@"notification.request: %@", notification.request);
  [RNCPushNotificationIOS didReceiveRemoteNotification:dict ];
}

我在 Xcode 控制台中打印出来的结果:

notification.request: <UNNotificationRequest: 0x28204e640; identifier: B1C6AF87-53AE-4AD0-BE68-A53E663ED2AA, content: <UNNotificationContent: 0x281504680; title: (null), subtitle: (null), body: <redacted>, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: , launchImageName: , threadIdentifier: , attachments: (
), badge: 1, sound: <UNNotificationSound: 0x2805001c0>, realert: 0, trigger: <UNPushNotificationTrigger: 0x282c18270; contentAvailable: NO, mutableContent: NO>>

Finally React Native 端我看到“通知”来了,显然上面描述的是空的。

如何在此处获取通知数据?

【问题讨论】:

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


    【解决方案1】:

    找到修复here

      NSDictionary *userInfo = notification.request.content.userInfo;
    
      [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:^void (UIBackgroundFetchResult result){}];
      completionHandler(UNNotificationPresentationOptionNone);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      • 2021-10-17
      • 2017-11-10
      • 2018-09-07
      • 2017-06-26
      • 1970-01-01
      • 2017-10-31
      相关资源
      最近更新 更多