react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
734 stars 282 forks source link

Automatically manage notifications object when app is in the foreground #302

Open strawberry-code opened 3 years ago

strawberry-code commented 3 years ago

Using react-native-push-notification-ios, I would like the notification to arrive, not be shown and the notification data to be passed to the React Native layer to the onNotification function.

To do this I have removed UNNotificationPresentationOptionAlert from the userNotificationCenter: willPresentNotification: withCompletionHandler function. However, I am not sure how to proceed for the object of the notification to be handled automatically delivered to React Native.

How can I do that?

The code presented below is what I am currently using. It works when notifications arrive, but they are not passed to 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);
}

Update 1

I found a way to handle directly the notification, however its contents seems to be empty:

The code I adjusted above:

    -(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 ];
    }

The result I get printed out in the Xcode console:

    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 side I see "notification" coming, obviously it is empty as described above.

How can I get notification data here?

jacquesdev commented 2 years ago

I suspect you are looking for notification.request.content.userInfo

cubabit commented 1 year ago

Hi - if you got this working could you share your solution? thanks!

usedlife commented 12 months ago

I suspect you are looking for notification.request.content.userInfo

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNNotificationPresentationOptionNone);
  [RNCPushNotificationIOS didReceiveRemoteNotification:notification.request.content.userInfo];
}

it works for me, thanks~

usedlife commented 12 months ago

Hi - if you got this working could you share your solution? thanks!

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNNotificationPresentationOptionNone);
  [RNCPushNotificationIOS didReceiveRemoteNotification:notification.request.content.userInfo];
}