在iOS8本地通知中设置动态按钮标题

时间:2015-07-09 17:10:38

标签: ios objective-c swift apple-push-notifications uilocalnotification

我已经按照这里和其他地方的示例来设置1或2个按钮本地通知,并且工作正常。如果您需要像Accept / Delete / etc这样的固定按钮,这些示例就可以了,但我需要为当时应用中发生的事情设置按钮标题。这适用于游戏,需要通用引擎。我需要能够设置按钮标题以符合当时的需求。我有两个类别,所以我可以做一个按钮或两个按钮通知。注册通知时,似乎需要设置操作和类别。 UIMutableUserNotificationCategory上的标题显示为"只读"。我知道这可以做到。例如游戏" Spy Watch"可以在90%的时间内播放通知。

在安排通知时是否设置了标题?我已经通过字典处理我需要处理响应的数据。

这是AppDelegate方

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];
}

以下是我在视图控制器中设置通知的方法

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];

0 个答案:

没有答案