Tuesday, May 31, 2016

Apple Watch handling Notification actions

First of all, we need to create categories

    UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
    acceptAction.identifier = @"ACCEPT_IDENTIFIER";
    acceptAction.title = @"Accept";
    acceptAction.activationMode = UIUserNotificationActivationModeBackground;
    acceptAction.destructive = NO;
    acceptAction.authenticationRequired = NO;
    
    UIMutableUserNotificationAction *rejectAction = [[UIMutableUserNotificationAction alloc] init];
    rejectAction.identifier = @"REJECT_IDENTIFIER";
    rejectAction.title = @"Reject";
    rejectAction.activationMode = UIUserNotificationActivationModeBackground;
    rejectAction.destructive = NO;
    rejectAction.authenticationRequired = NO;
    
    UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
    notificationCategory.identifier = @"CALL_NOTIFICATION_CATEGORY";
    [notificationCategory setActions:@[acceptAction,rejectAction]forContext:UIUserNotificationActionContextDefault];
    
    NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:categories];
    

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

Now on the App Delegate of the companion app, can override the method


- (void)application:(UIApplication *) application handleActionWithIdentifier: (NSString *) identifier forLocalNotification: (NSDictionary *) notification completionHandler: (void (^)()) completionHandler {

if ([identifier isEqualToString: @"ACCEPT_IDENTIFIER"])
        {
            NSLog(@"Accept is pressed");
            OngoingCallScreen *callScreen = [OngoingCallScreen sharedScreen];
            NSArray *callSessions = [callScreen getActiveCalls];
            NSLog(@"Call Sessions Are :%@",callSessions);
            if([callSessions count] > 0)
            {
                [callScreen handleCallEvent:CallEventToAnswerIncomingCall with:[callSessions objectAtIndex:0]];
            }
            
        }
        else
        {
            NSLog(@"Reject is pressed");
            OngoingCallScreen *callScreen = [OngoingCallScreen sharedScreen];
            NSArray *callSessions = [callScreen getActiveCalls];
            NSLog(@"Call Sessions Are :%@",callSessions);
            if([callSessions count] > 0)
            {
                [callScreen handleCallEvent:CallEventToRejectIncomingCall with:[callSessions objectAtIndex:0]];
            }
        }
   completionHandler();
}

references:
https://developer.apple.com/library/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/BasicSupport.html#//apple_ref/doc/uid/TP40014969-CH18-SW1

No comments:

Post a Comment