Setting dynamic button names in local notifications iOS8

I've followed examples here and elsewhere for setting local notifications to 1 or 2 buttons and they work fine. The examples are very good if you want canned buttons like Accept / Delete / etc, but I need to set the button names for what is happening in the application at the time. This is for the game and requires a universal engine. I need to be able to set the button names according to the needs at the time. I have two categories so I can make one button or two notification buttons. It seems like actions and categories need to be configured when registering for notification. The title in UIMutableUserNotificationCategory is displayed as read-only. I know it can be done. For example, the game "Spy Clock" can be played 90% of the time from notifications.

Are there any headers set when scheduling a notification? I passed in dictionaries with data that 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];
}

      

This is how I set up the notification in the 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];

      

+3


source to share





All Articles