Last active
August 29, 2015 14:10
-
-
Save sdgandhi/576f10c3c43626ee8d5f to your computer and use it in GitHub Desktop.
iOS 8 Notification Actions Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Borrowed some stuff from the WWDC 2014 video | |
// Create the action | |
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init]; | |
acceptAction.identifier = @"ACCEPT_IDENTIFIER"; | |
acceptAction.title = @"Accept"; | |
acceptAction.activationMode = UIUserNotificationActivationModeBackground; // If we need to show UI, use UIUserNotificationActivationModeForeground | |
acceptAction.destructive = NO; // If YES, shows red button | |
acceptAction.authenticationRequired = NO; // If YES, requires passcode | |
// Create a category for the action | |
UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init]; | |
inviteCategory.identifier = @"INVITE_CATEGORY"; // We will set this in our notification payload | |
[inviteCategory setActions:@[ acceptAction, maybeAction, declineAction ] forContext:UIUserNotificationActionContextDefault]; | |
// Default context used when we can have up to 4 actions, if we only have space for 2, we only see the first two | |
// When we can only have 2, we need to use UIUserNotificationActionContextMinimal to define exactly what we see | |
// Register categories | |
NSSet *categories = [NSSet setWithObjects:inviteCategory, alarmCategory]; | |
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories]; | |
[[UIApplicationSharedApplication] registerUserNotificationSettings:settings]; | |
// Creating the notification | |
// Remote (payload size now 2kb!) | |
{ | |
"aps" : { | |
"alert" : "You're invited!", | |
"category" : "INVITE_CATEGORY", // Remember we created this identifier earlier | |
} | |
} | |
// Local | |
UILocalNotification *notification = ... | |
notification.category = @"INVITE_CATEGORY"; | |
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; | |
// Handling Actions | |
// For default action (just a tap) | |
UIApplicationDelegate application:didFinishLaunchingWithOptions: | |
application:didReceiveRemoteNotification:fetchCompletionHandler: | |
// For new actions | |
UIApplicationDelegate | |
- (void)handleActionWithIdentifier:forLocalNotification:completionHandler { | |
if(identifier ...) { | |
[self handleAccpeActionWithNotification:notification]; | |
} | |
completionHandler(); // We have to call this to finish | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment