FRANotificationHandler.m revision a1e92b2783be4bfeb0c7e267223cc7779a6f324c
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * The contents of this file are subject to the terms of the Common Development and
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * Distribution License (the License). You may not use this file except in compliance with the
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * specific language governing permission and limitations under the License.
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * When distributing Covered Software, include this CDDL Header Notice in each file and include
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * Header, with the fields enclosed by brackets [] replaced by your own identifying
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * information: "Portions copyright [year] [name of copyright owner]".
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * Copyright 2016 ForgeRock AS.
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell#import "FRABlockAlertView.h"
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell#import "FRAIdentity.h"
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell#import "FRAIdentityModel.h"
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell#import "FRAPushMechanism.h"
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell#import "FRANotification.h"
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell#import "FRANotificationHandler.h"
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell#import "FRANotificationViewController.h"
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings#import "FRAMessageUtils.h"
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * Private interface.
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell * The identity model.
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell@property (nonatomic, strong, readonly) FRAIdentityModel *identityModel;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell * The database to which notifications should be persisted.
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell@property (strong, nonatomic) FRAIdentityDatabase *database;
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbingsstatic NSString * const MESSAGE_ID_KEY_PATH = @"aps.messageId";
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbingsstatic NSString * const MECHANISM_UID_KEY = @"u";
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (instancetype)initWithDatabase:(FRAIdentityDatabase *)database identityModel:(FRAIdentityModel *)identityModel {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell+ (instancetype)handlerWithDatabase:(FRAIdentityDatabase *)database identityModel:(FRAIdentityModel *)identityModel {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell return [[FRANotificationHandler alloc] initWithDatabase:database identityModel:identityModel];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)messageData {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell FRANotification *notification = [self notificationFromRemoteNotification:messageData];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell if (!notification || !notification.pending) {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // if the notification is nil then there was a problem looking up the mechanism
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // if the notification is not pending, then the notification timed out or was dealt with by opening the app
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // from the homescreen and navigating to the notification; either way, there's nothing further to do here
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell [self showNotification:notification ifForegroundApplication:application];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell- (FRANotification *)notificationFromRemoteNotification:(NSDictionary *)messageData {
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings // Decode JWT from modessage
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings NSString* alertJwt = [messageData valueForKeyPath:@"aps.alert"];
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings NSDictionary *payload = [FRAMessageUtils extractJTWBodyFromString:alertJwt];
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings // makec mechanism from data
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // Lookup push mechanism to which the notification should be added
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings FRAPushMechanism *mechanism = [self pushMechanismTargetForRemoteNotification:payload];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // Try to look up an existing notification (in case this message has already been handled)
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings FRANotification *notification = [mechanism notificationWithMessageId:[messageData valueForKeyPath:MESSAGE_ID_KEY_PATH]];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // otherwise, create the notification from the message and add it to the mechanism
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings NSTimeInterval timeToLive = [[payload objectForKey:TTL_KEY] doubleValue];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell notification = [[FRANotification alloc] initWithDatabase:self.database
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings messageId:[messageData valueForKeyPath:MESSAGE_ID_KEY_PATH]
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings challenge:[payload objectForKey:CHALLENGE_KEY]
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings // TODO: Handle error
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott [mechanism addNotification:notification error:&error];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell- (FRAPushMechanism *)pushMechanismTargetForRemoteNotification:(NSDictionary *)messageData {
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings NSString *mechanismId = [messageData objectForKey:MECHANISM_UID_KEY];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell FRAMechanism *mechanism = [self.identityModel mechanismWithId:mechanismId];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell if ([mechanism isKindOfClass:[FRAPushMechanism class]]) {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell- (void)showNotification:(FRANotification *)notification ifForegroundApplication:(UIApplication *)application {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // jump to the notification if the app was in the foreground when the notification arrived or has
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // been opened by clicking on the notification
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell UIViewController *rootViewController = application.delegate.window.rootViewController;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell UIStoryboard *storyboard = rootViewController.storyboard;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell FRANotificationViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:FRANotificationViewControllerStoryboardIdentifer];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell [rootViewController presentViewController:viewController animated:YES completion:NULL];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // don't show notification if app is running in background
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // the notification arrived while the app was in the foreground
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell FRABlockAlertView *alertView = [[FRABlockAlertView alloc] initWithTitle:@"Authentication Request Received"
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // the application is being opened from the notification