pushy / pushy-react-native

The official Pushy SDK for React Native apps.
Apache License 2.0
16 stars 20 forks source link

Ios notification with background data enabled #34

Closed bohrend closed 4 years ago

bohrend commented 4 years ago

Hi, I have logic in my notificationListener that I want fired, it doesn't have to show the notification in notification panel (so basically a silent push). Its a react native app, I understand ios notifications don't show when the app is in the foreground, but I came across this in the pushy documentation:

content_available

When set to true, your app's notification handler will be invoked even if the app is running in the background, making it possible to fetch updated content from the server or execute other custom logic without necessarily alerting the user.

Requires the Background Modes -> Remote Notifications capability to be enabled.

Should this enabled the logic in notificationListener to fire? if not, could you recommend or guide me in the right direction, thanks!

pushy commented 4 years ago

Hi @bohrend, Apologies for the delayed response.

You will need to implement your notification listener in native code to achieve silent push functionality for iOS on React Native. This is an iOS platform restriction, as iOS does not support React Native's headless JS feature.

Add the following lines to your ios/app/AppDelegate.m file, anywhere inside your app's didFinishLaunchingWithOptions method:

// Handle silent push notifications
[pushy setNotificationHandler:^(NSDictionary *data, void (^completionHandler)(UIBackgroundFetchResult)) {
    // Print notification payload data
    NSLog(@"Received notification: %@", data);

    // Custom logic...

    // You must call this completion handler when you finish processing
    // the notification (after fetching background data, if applicable)
    completionHandler(UIBackgroundFetchResultNewData);
}];

Please add the required import statement:

#import <Pushy/Pushy-Swift.h>

Make sure to delete the notification listener defined in your RN JS app (Pushy.setNotificationListener()), and enable the Background Modes -> Remote Notifications capability in your Xcode project settings.