I am trying to implement Interactive UILocalNotifications.
Following is my code. I am unable to get 3 Action Buttons of receiving Notification.
UIMutableUserNotificationAction *nAction1 = [[UIMutableUserNotificationAction alloc] init];
nAction1.identifier = #"Present";
nAction1.title = #"Present";
nAction1.activationMode = UIUserNotificationActivationModeBackground;
nAction1.destructive = NO;
nAction1.authenticationRequired = YES;
UIMutableUserNotificationAction *nAction2 = [[UIMutableUserNotificationAction alloc] init];
nAction2.identifier = #"Late";
nAction2.title = #"Late";
nAction2.activationMode = UIUserNotificationActivationModeBackground;
nAction2.destructive = NO;
nAction2.authenticationRequired = YES;
UIMutableUserNotificationAction *nAction3 = [[UIMutableUserNotificationAction alloc] init];
nAction3.identifier = #"Absent";
nAction3.title = #"Absent";
nAction3.activationMode = UIUserNotificationActivationModeBackground;
nAction3.destructive = YES;
nAction3.authenticationRequired = YES;
UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = #"Attendance";
[notificationCategory setActions:#[nAction3, nAction2, nAction1] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:#[nAction3, nAction1] forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];
UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.alertBody = #"Testing";
localNotification.category = #"Attendance";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[notificationCategory setActions:#[nAction3, nAction2, nAction1] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:#[nAction3, nAction1] forContext:UIUserNotificationActionContextDefault];
It seems that you are overriding the same context with just 2 buttons I guess that the correct should be:
[notificationCategory setActions:#[nAction3, nAction2, nAction1] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:#[nAction3, nAction1] forContext:UIUserNotificationActionContextMinimal];
Related
I create UILocalNotification and UIMutableUserNotificationAction
if I use iphone, this action button it seems BUT I use ipad this action button does not seems
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc]init];
action.identifier = #"ActionNotification";
action.title = #"START";
action.activationMode = UIUserNotificationActivationModeForeground;
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc]init];
categorys.identifier = #"alert";
[categorys setActions:#[action] forContext:UIUserNotificationActionContextMinimal];
UIUserNotificationSettings *setings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:[NSSet setWithObjects:categorys, nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:setings];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = aIntervalDate;
//localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.alertBody =#"Notification Test";
localNotification.alertAction=#"START";
localNotification.category=#"alert";
localNotification.soundName = #"Reminder_Sound.mp3";
localNotification.applicationIconBadgeNumber=1;
// localNotification.hasAction=YES;
NSDictionary *infoDict = #{#"title" : #"xxxxxx",
#"tipi" : #"yyyyy"};
localNotification.userInfo = infoDict;
localNotification.timeZone = [NSTimeZone systemTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
I want to implement interactive notification with two buttons with actions. Where I have to create buttons and their actions?
I have done this but from where I have to call this method?
- (void)registerForNotification
{
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:#"Action 1"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];
[action2 setTitle:#"Action 2"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:#[action1, action2]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
func showInterActiveNotification() {
let category = UIMutableUserNotificationCategory()
let restartAction = UIMutableUserNotificationAction()
restartAction.identifier = "xx"
restartAction.destructive = false
restartAction.title = "Restart"
restartAction.activationMode = .Background
restartAction.authenticationRequired = false
let categoryIdentifier = "category.identifier"
category.identifier = categoryIdentifier
category.setActions([restartAction], forContext: .Minimal)
category.setActions([restartAction], forContext: .Default)
let categories = Set(arrayLiteral: category)
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: categories)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
let localNotif = UILocalNotification()
localNotif.alertBody = "testBody"
localNotif.category = categoryIdentifier
// Notification will be shown after 10 second (IMPORTANT: if you want to see notification you have to close or put app into background)
localNotif.fireDate = NSDate().dateByAddingTimeInterval(10)
UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
}
Handle Notification
func application(application: UIApplication, handleActionWithIdentifier identifier: String?,
forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
completionHandler()
}
Call this in didFinishLaunchingWithOptions method in AppDelegate. Nothing else you need to do.
To handle the actions call the handler.
if you want to check whith local notification plz use this
-(void)localnotificationGenrated
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.soundName = #"Default";
localNotification.alertBody = #"my notification;)";
localNotification.fireDate = [NSDate date];
localNotification.category = NotificationActionOneIdent;
localNotification.repeatInterval = kCFCalendarUnitMinute;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
and if you want with push notification create payload with category.
like this
{"aps":{"alert":"Hello Testing","badge":1,"sound":"default","category":"your_category_key"}}
and call your method
- (void)registerForNotification
in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UIMutableUserNotificationAction *answer;
answer = [[UIMutableUserNotificationAction alloc] init];
[answer setActivationMode:UIUserNotificationActivationModeBackground];
[answer setTitle:#"Answer"];
[answer setIdentifier:#"Answer"];
[answer setDestructive:NO];
[answer setAuthenticationRequired:NO];
[answer setBehavior:UIUserNotificationActionBehaviorTextInput];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:#[answer]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
This is how I set up my "Reply" action. As you can see, I specified the desired behavior. However, when I receive some notification, I pull it down and see this "Answer" button. What's the problem? Where is the textfield?
I have followed the examples here and elsewhere for setting up 1 or 2 button local notifications and have that working just fine. The examples are just fine if you need canned buttons like Accept/Delete/etc, but I need need to set the button titles for what is happening in the app at the time. This is for a game and needs a generic engine. I need to be able to set the button titles to match the needs at the time. I have two categories so I can do one button or two button notification. It seems like the actions and categories need to be setup when you register for the notification. The title on the UIMutableUserNotificationCategory is shown as "read only". I know this can be done. For example the game "Spy Watch" can be played 90% of the time from the notifications.
Is there anyway set the titles when scheduling the notification? I have passed dictionaries with the data I need to process the response.
This is the AppDelegate side
NSString * const NotificationCategoryIdent1 = #"1BUTTON";
NSString * const NotificationCategoryIdent2 = #"2BUTTON";
NSString * const NotificationActionOneIdent = #"ACTION_ONE";
NSString * const NotificationActionTwoIdent = #"ACTION_TWO";
- (void)registerForNotification {
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:#"Button 1"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];
[action2 setTitle:#"Button 2"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory2;
actionCategory2 = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory2 setIdentifier:NotificationCategoryIdent2];
[actionCategory2 setActions:#[action1, action2]
forContext:UIUserNotificationActionContextDefault];
UIMutableUserNotificationCategory *actionCategory1;
actionCategory1 = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory1 setIdentifier:NotificationCategoryIdent1];
[actionCategory1 setActions:#[action1]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObjects:actionCategory1, actionCategory2, nil];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
Here is how I set up the notification in my view controller
UILocalNotification* localNotification2 = [[UILocalNotification alloc] init];
localNotification2.fireDate = [NSDate dateWithTimeIntervalSinceNow:30];
localNotification2.alertBody = #"Alert Body2";
localNotification2.timeZone = [NSTimeZone defaultTimeZone];
localNotification2.soundName = UILocalNotificationDefaultSoundName;
localNotification2.category = #"2BUTTON";
//localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
I'm trying to schedule an interactive UILocalNotifaction.
My attempt has been to use the following code, which I grabbed from this tutorial:
NSString * const NotificationCategoryIdent = #"ACTIONABLE";
NSString * const NotificationActionOneIdent = #"ACTION_ONE";
NSString * const NotificationActionTwoIdent = #"ACTION_TWO";
- (void)registerForNotification {
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:#"Action 1"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];
[action2 setTitle:#"Action 2"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:#[action1, action2]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
However, this code doesn't seem to actually schedule the notification anywhere. What am I missing?
Apollo,
Scheduling a local notification is relatively easy. Piggy backing off the tutorial you referenced I kind of switched it up so it can make more sense, you can deter or alter the code as need be, but this will give you a better starting point than the tutorial did. Please know it's not mandatory to put this in application didFinishLaunchingWithOptions: you can schedule a UILocalNotification anywhere app-wide. See the references below for complete properties of each method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Here we are just going to create the actions for the category.
UIMutableUserNotificationAction *acceptAction = [self createAction];
UIMutableUserNotificationAction *laterAction = [self createLaterAction];
laterAction.title = #"Not Now";
//Creating the category and assigning the actions:
UIMutableUserNotificationCategory *acceptCategory = [self createCategory:#[acceptAction, laterAction]];
//Register the categories
[self registerCategorySettings:acceptCategory];
//For testing purposes we will just fire a local notification on load. This is just for reference, but you don't have to call it in applicationDidFinishLaunching
[self fireLocalNotification];
return YES;
}
//Create a category
- (UIMutableUserNotificationCategory *)createCategory:(NSArray *)actions {
UIMutableUserNotificationCategory *acceptCategory = [[UIMutableUserNotificationCategory alloc] init];
acceptCategory.identifier = #"ACCEPT_CATEGORY";
[acceptCategory setActions:actions forContext:UIUserNotificationActionContextDefault];
return acceptCategory;
}
//Register your settings for that category
- (void)registerCategorySettings:(UIMutableUserNotificationCategory *)category {
UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
NSSet *categories = [NSSet setWithObjects:category, nil];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
//Create Actions Methods
- (UIMutableUserNotificationAction *)createAction {
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = #"ACCEPT_IDENTIFIER";
acceptAction.title = #"Accept";
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;
// If YES requires passcode
acceptAction.authenticationRequired = NO;
return acceptAction;
}
- (UIMutableUserNotificationAction *)createLaterAction {
UIMutableUserNotificationAction *laterAction = [[UIMutableUserNotificationAction alloc] init];
laterAction.identifier = #"LATER_IDENTIFIER";
laterAction.title = #"Not Now";
laterAction.activationMode = UIUserNotificationActivationModeBackground;
laterAction.destructive = NO;
laterAction.authenticationRequired = NO;
return laterAction;
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
UIUserNotificationType allowedTypes = [notificationSettings types];
NSLog(#"Registered for notification types: %u", allowedTypes);
}
//Fire a local Notification: For testing purposes we are just going to fire this immediately after launch. But this will give you a general idea of how to create a UILocalNotification app-wide:
- (void)fireLocalNotification {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"We are just testing -";
notification.category = #"ACCEPT_CATEGORY";
notification.fireDate = [NSDate dateWithTimeInterval:15 sinceDate:[NSDate date]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
And your outcome will be as anticipated:
Don't forget to call the completion handlers for the actions that were selected.
application:handleActionWithIdentifier:forLocalNotification:completionHandler:
Please review the documentation and references below for further customization. Again, i'm not saying this is the right way and it's definetly not the only, it's just a great starting point for you to understand how they work. There are lots of customizable properties for actions and UILocalNotifications
REFERENCES
UILocalNotification & Properties : Documentation
iOS8 Notification Actions & Properties : Documentation