Interactive notifications (When the screen is locked) All Action.
Interactive notifications (When the screen is locked) All Action.
STEP:-1 We have to do code only AppDelegate.m file
NSString* const OkButton = @"RegularButtonOKAction";
NSString* const cancelButton = @"RegularButtonCancelAction";
NSString* const textInput = @"TextInputOkAction";
NSString* const textInputCancel = @"TextInputCancelAction";
NSString* const categryIdent = @"categryIdent";
NSString* const textcategryIdent = @"textcategryIdent";
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
[self registerForNotification];
return YES;
}
- (void)application:(UIApplication*)application
handleActionWithIdentifier:(nullable NSString*)identifier
forRemoteNotification:(NSDictionary*)userInfo
withResponseInfo:(NSDictionary*)responseInfo
completionHandler:(void (^) ())completionHandler {
if ([identifier isEqualToString:cancelButton]) {
NSLog (@"User Pressed Cancel Button");
} else if ([identifier isEqualToString:OkButton]) {
NSLog (@"User Pressed OK Button");
} else if ([identifier isEqualToString:textInputCancel]) {
NSLog (@"User Pressed Cancel button for text input action");
} else if ([identifier isEqualToString:textInput]) {
NSString* comment = responseInfo[UIUserNotificationActionResponseTypedTextKey];
if (comment.length > 0) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"User Entered Input"
message:comment
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
[alert show];
NSLog (@"
textfiels text is == %@", comment);
textfiels text is == %@", comment);
}
}
if (completionHandler) {
completionHandler ();
}
}
- (void)application:(UIApplication*)application
handleActionWithIdentifier:(NSString*)identifier
forRemoteNotification:(NSDictionary*)userInfo
completionHandler:(void (^) ())completionHandler {
if ([identifier isEqualToString:cancelButton]) {
NSLog (@"User Pressed Cancel Button");
} else if ([identifier isEqualToString:OkButton]) {
NSLog (@"User Pressed OK Button");
} else if ([identifier isEqualToString:textInputCancel]) {
NSLog (@"User Pressed Cancel button for text input action");
} else if ([identifier isEqualToString:textInput]) {
NSLog (@"User Pressed OK button for text input action");
}
if (completionHandler) {
completionHandler ();
}
}
#pragma Code to setup push notifications.
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSString* token = [NSString stringWithFormat:@"%@", deviceToken];
// Format token as you need:
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
NSLog (@"%@", token);
}
- (void)registerForNotification {
UIMutableUserNotificationAction* action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:@"Cancel"];
[action1 setIdentifier:cancelButton];
[action1 setDestructive:YES];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationAction* action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeForeground];
[action2 setTitle:@"OK"];
[action2 setBehavior:UIUserNotificationActionBehaviorDefault];
[action2 setIdentifier:OkButton];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:YES];
UIMutableUserNotificationCategory* actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:categryIdent];
[actionCategory setActions:@[ action1, action2 ] forContext:UIUserNotificationActionContextDefault];
UIMutableUserNotificationAction* action3;
action3 = [[UIMutableUserNotificationAction alloc] init];
[action3 setActivationMode:UIUserNotificationActivationModeBackground];
[action3 setTitle:@"Cancel"];
[action3 setIdentifier:textInputCancel];
[action3 setDestructive:YES];
[action3 setAuthenticationRequired:NO];
UIMutableUserNotificationAction* action4;
action4 = [[UIMutableUserNotificationAction alloc] init];
[action4 setActivationMode:UIUserNotificationActivationModeBackground];
[action4 setTitle:@"OK"];
[action4 setBehavior:UIUserNotificationActionBehaviorTextInput];
[action4 setIdentifier:textInput];
[action4 setDestructive:NO];
[action4 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory* actionCategory2;
actionCategory2 = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory2 setIdentifier:textcategryIdent];
[actionCategory2 setActions:@[ action3, action4 ] forContext:UIUserNotificationActionContextDefault];
NSSet* categories = [NSSet setWithObjects:actionCategory, actionCategory2, nil];
UIUserNotificationType types =
(UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
NSLog (@"Received Push notification with user info %@", userInfo);
}
Step 2:- The most important point is when we send push that time we have to add "category" in push dictionary
- 1. If we have need textbox then set category = textcategryIdent
- 2. if we have need only action button then set category = categryIdent
Here is code
https://github.com/8109350789/interactive-notifications
Comments
Post a Comment