globocom / react-native-ua

React Native module for Urban Airship
MIT License
37 stars 33 forks source link

CHANGE: update UrbarAirshipSDK to 8.0 to support iOS10 #23

Closed emilianoeloi closed 8 years ago

emilianoeloi commented 8 years ago

iOS 10 support #22 urbarAirshipSDK 8.0.1

rlepinski commented 8 years ago

You need to update the PushNotificationDelegate methods -http://docs.urbanairship.com/reference/libraries/ios/latest/Protocols/UAPushNotificationDelegate.html

rlepinski commented 8 years ago

I believe you want something like this:

-(void)receivedBackgroundNotification:(UANotificationContent *)notificationContent completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    if (![self actionHandleNotification:notificationContent.notificationInfo completionHandler:completionHandler]) {
        [[ReactNativeUAIOS getInstance] dispatchEvent:@"receivedNotification" body:@{@"event": @"receivedBackgroundNotificationActionButton",
                                                                                     @"data": notificationContent.notificationInfo}];

        completionHandler(UIBackgroundFetchResultNoData);
    }

}

-(void)receivedForegroundNotification:(UANotificationContent *)notificationContent completionHandler:(void (^)())completionHandler {

    if (![self actionHandleNotification:notificationContent.notificationInfo completionHandler:completionHandler]) {
        [[ReactNativeUAIOS getInstance] dispatchEvent:@"receivedNotification" body:@{@"event": @"receivedForegroundNotification",
                                                                                     @"data": notificationContent.notificationInfo}];

        completionHandler();
    }
}

-(void)receivedNotificationResponse:(UANotificationResponse *)notificationResponse completionHandler:(void (^)())completionHandler {

    if (![self actionHandleNotification:notificationResponse.notificationContent.notificationInfo completionHandler:completionHandler]) {

        if ([notificationResponse.actionIdentifier isEqualToString:UANotificationDefaultActionIdentifier]) {
            [[ReactNativeUAIOS getInstance] dispatchEvent:@"receivedNotification" body:@{@"event": @"launchedFromNotification",
                                                                                         @"data": notification}];

            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

            [defaults setObject:notification forKey:@"push_notification_opened_from_background"];
            [defaults synchronize];
        } else {

            UANotificationAction *notificationAction = [self notificationActionForCategory:response.notificationContent.categoryIdentifier
                                                                          actionIdentifier:response.actionIdentifier];

            if (notificationAction.options & UNNotificationActionOptionForeground) {
                [[ReactNativeUAIOS getInstance] dispatchEvent:@"receivedNotification" body:@{@"event": @"launchedFromNotificationActionButton",
                                                                                             @"data": notificationResponse.notificationContent.notificationInfo}];
            } else {

                [[ReactNativeUAIOS getInstance] dispatchEvent:@"receivedNotification" body:@{@"event": @"receivedBackgroundNotificationActionButton",
                                                                                             @"data": notificationResponse.notificationContent.notificationInfo}];

            }

        }

        completionHandler();

    }

}

+ (UANotificationAction *)notificationActionForCategory:(NSString *)category actionIdentifier:(NSString *)identifier {
    NSSet *categories = [UAirship push].combinedCategories;

    UANotificationCategory *notificationCategory;
    UANotificationAction *notificationAction;

    for (UANotificationCategory *possibleCategory in categories) {
        if ([possibleCategory.identifier isEqualToString:category]) {
            notificationCategory = possibleCategory;
            break;
        }
    }

    if (!notificationCategory) {
        return nil;
    }

    NSMutableArray *possibleActions = [NSMutableArray arrayWithArray:notificationCategory.actions];

    for (UANotificationAction *possibleAction in possibleActions) {
        if ([possibleAction.identifier isEqualToString:identifier]) {
            notificationAction = possibleAction;
            break;
        }
    }

    return notificationAction;
}

I have to run for the day, but I can follow up tomorrow if you need more help. I was unable to test it and I am sure you can simplify the notificationResponse method.

emilianoeloi commented 8 years ago

Great! Thank you, Mr. Lepinski.

rlepinski commented 8 years ago

@emilianoeloi https://github.com/emilianoeloi/react-native-ua/pull/1

rlepinski commented 8 years ago

@emilianoeloi - You can also provide default presentation options on UAPush .

We did not add support for the options in UAConfig, but you could have the plugin look for some flags in the customConfig section of AirshipConfig and set the default options for the plugin.