操作和类别不会在iOS中的UILocalNotification中显示 [英] actions and categories don't show in UILocalNotification in iOS
问题描述
我有以下来自苹果文档的信息这里是我需要有一个类别 UILocalNotification
:
I have the following which I believe from the apple documentation here is all I need to have a category for UILocalNotification
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];
// Define an ID string to be passed back to your app when you handle the action
acceptAction.identifier = @"ACCEPT_IDENTIFIER";
// Localized string displayed in the action button
acceptAction.title = @"Accept";
// If you need to show UI, choose foreground
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
// Destructive actions display in red
acceptAction.destructive = NO;
// Set whether the action requires the user to authenticate
acceptAction.authenticationRequired = NO;
// First create the category
UIMutableUserNotificationCategory *inviteCategory =
[[UIMutableUserNotificationCategory alloc] init];
// Identifier to include in your push payload and local notification
inviteCategory.identifier = @"INVITE_CATEGORY";
// Add the actions to the category and set the action context
[inviteCategory setActions:@[acceptAction]
forContext:UIUserNotificationActionContextDefault];
// Set the actions to present in a minimal context
[inviteCategory setActions:@[acceptAction]
forContext:UIUserNotificationActionContextMinimal];
NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
// Handle launching from a notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(@"Recieved Notification %@",localNotif);
}
return YES;
}
这是我如何构建我的本地通知:
Here is how I construct my local notification:
NSDate *dateChosen = [self.reminderDatePicker date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:dateChosen];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
// NSCalendar *calendar = [NSCalendar currentCalendar];
// NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: 3];
[components setMonth: 7];
[components setYear: 2012];
[components setHour: hour];
[components setMinute: minute];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateToFire;
[localNotification setRepeatInterval: kCFCalendarUnitDay];
NSLog(@"Notification will be shown on: %@ ",localNotification.fireDate);
localNotification.userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:self.dayPeriod, @"name", nil];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = alertMessage;
localNotification.alertAction = NSLocalizedString(@"View details", nil);
localNotification.repeatInterval = NSDayCalendarUnit;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = -1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
它只显示了关闭按钮而不是我的接受按钮的默认通知。
It just shows the default notification with a dismiss button and not my accept button.
如何在我的本地通知中显示此「接受」按钮?
How can I get this "accept" button to show on my local notification?
推荐答案
你的主体代码 UILocalNotification
中丢失的一个事实是明确告诉它是一个基于类别的通知。
The one thing that your missing in your main body of code for UILocalNotification
is explicitly telling it to be a category based notification.
在您的通知中添加类别属性:
Add the category property to your notification:
localNotification.category = @"INVITE_CATEGORY";
这篇关于操作和类别不会在iOS中的UILocalNotification中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!