iOS 的 UILocalNotification 中不显示操作和类别

Posted

技术标签:

【中文标题】iOS 的 UILocalNotification 中不显示操作和类别【英文标题】:actions and categories don't show in UILocalNotification in iOS 【发布时间】:2015-08-27 00:58:01 【问题描述】:

我相信苹果文档here中的以下内容是我需要为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;

这是我构建本地通知的方式:

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

它只显示带有关闭按钮的默认通知,而不是我的接受按钮。

如何让这个“接受”按钮显示在我的本地通知中?

【问题讨论】:

您能展示一下您是如何构建本地通知的吗?其他一切似乎都到位 @soulshined 我加了。 【参考方案1】:

您在UILocalNotification 的主体代码中缺少的一件事是明确告诉它是基于类别的通知。

将类别属性添加到您的通知中:

localNotification.category = @"INVITE_CATEGORY";

【讨论】:

以上是关于iOS 的 UILocalNotification 中不显示操作和类别的主要内容,如果未能解决你的问题,请参考以下文章

UILocalNotification.applicationIconBadgeNumber 在 iOS 5 中不起作用!!!

iOS8 - UILocalNotification,稍后从代码中检查权限

UILocalNotification 在 iOS11 中不起作用

iOS 的 UILocalNotification 中不显示操作和类别

iOS开发---本地通知(UILocalNotification)

iOS9 的 UILocalNotification 问题