iOS 自定义按钮不显示交互式通知

标签 ios ios8 uilocalnotification

我目前正在处理由 8 制作的交互式 iOS 通知,老实说,我真的很难看到我的按钮。 使用下面的代码,我看不到我的按钮:

//互动

UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];

acceptAction.identifier = @"ACCEPT_IDENTIFIER";
acceptAction.title = @"Accept";
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;
acceptAction.authenticationRequired = NO;

UIMutableUserNotificationAction *maybeAction =
[[UIMutableUserNotificationAction alloc] init];

maybeAction.identifier = @"MAYBE_IDENTIFIER";
maybeAction.title = @"Maybe";
maybeAction.activationMode = UIUserNotificationActivationModeBackground;
maybeAction.destructive = NO;
maybeAction.authenticationRequired = YES;


UIMutableUserNotificationAction *declineAction =
[[UIMutableUserNotificationAction alloc] init];

declineAction.identifier = @"DECLINE_IDENTIFIER";
declineAction.title = @"Decline";
declineAction.activationMode = UIUserNotificationActivationModeBackground;
declineAction.destructive = YES;
declineAction.authenticationRequired = NO;


UIMutableUserNotificationCategory *inviteCategory =
[[UIMutableUserNotificationCategory alloc] init];

inviteCategory.identifier = @"INVITE_CATEGORY";

[inviteCategory setActions:@[acceptAction, maybeAction, declineAction]
                forContext:UIUserNotificationActionContextDefault];

[inviteCategory setActions:@[acceptAction, declineAction]
                forContext:UIUserNotificationActionContextMinimal];


NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = @"Testing";
localNotification.category = @"INVITE_CATEGORY"; //  Same as category identifier
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

对我的问题有什么想法吗? 尽管 Internet 上有所有示例,但我显然是唯一遇到此问题的人。

最佳答案

这里有一个非常棒的教程:The Code Ninja

您代码中的一切看起来都很好。我必须要求确保您没有忽略实际发生的事情 - 如果您正在查看主屏幕上的通知,您是否在通知上向左滑动以显示按钮?

这是我正在使用的(对不起,swift)

通知的初始提示

    var openAction = UIMutableUserNotificationAction()
    openAction.identifier = "OPEN_IDENTIFIER"
    openAction.title = "Open"
    openAction.destructive = false
    openAction.authenticationRequired = false
    openAction.activationMode = .Foreground

    var snoozeAction = UIMutableUserNotificationAction()
    snoozeAction.identifier = "SNOOZE_IDENTIFIER"
    snoozeAction.title = "Snooze"
    snoozeAction.destructive = true
    snoozeAction.authenticationRequired = false
    snoozeAction.activationMode = .Background

    var snoozeable = UIMutableUserNotificationCategory()
    snoozeable.identifier = NOTE_SNOOZE_CAT
    snoozeable.setActions([openAction, snoozeAction], forContext: .Default)
    snoozeable.setActions([openAction, snoozeAction], forContext: .Default)

    var notSnoozable = UIMutableUserNotificationCategory()
    notSnoozable.identifier = NOTE_NO_SNOOZE_CAT
    notSnoozable.setActions([openAction], forContext: .Default)
    notSnoozable.setActions([openAction], forContext: .Default)

    UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert |
        .Badge, categories: NSSet(array:[snoozeable, notSnoozable])
        ))

本地通知

    var notification             = UILocalNotification()
    notification.fireDate        = fireDate
    notification.timeZone        = NSTimeZone.defaultTimeZone()
    notification.alertBody       = "Alert Body"
    notification.userInfo        = userInfo
    notification.category        = blnCanSnooze ? NOTE_SNOOZE_CAT : NOTE_NO_SNOOZE_CAT
    UIApplication.sharedApplication().scheduleLocalNotification(notification)

关于iOS 自定义按钮不显示交互式通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26125693/

相关文章:

ios - AdMob 和 AdWhirl 与 iOS6 混淆

ios - 如何在 Swift 的第一个 ViewController 中隐藏导航栏?

ios - 出现弹出窗口时,在 iOS 8 中禁用 UISplitViewController 纵向到横向旋转

objective-c - 为什么 Reachability 类会为 iOS8 返回意想不到的结果?

ios - 谷歌地图中的 "My location button"消失了

ios - 如何通知在多个设备上运行的 iOS 应用程序?

iphone - 在 App 中持久保存图像 - iOS

objective-c - iOS 私有(private)框架

ios - 重新启动应用程序或关闭应用程序后,计划的 UILocalNotification 不会触发

ios - 通过 soundID 获取 soundName 将其分配给 UILocalNotification