【问题标题】:iOS Custom buttons not showing for interactive notificationiOS 自定义按钮未显示交互式通知
【发布时间】:2014-09-30 16:29:22
【问题描述】:

我目前正在研究由 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];

对我的问题有什么想法吗? 尽管互联网上有很多例子,但我显然是唯一一个遇到这个问题的人。

【问题讨论】:

    标签: ios ios8 uilocalnotification


    【解决方案1】:

    这里有一个非常棒的教程: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)
    

    【讨论】:

    • 我认为在第二个snoozeable.setActions([openAction, snoozeAction], forContext: .Default) 中,您的意思是将.Default 替换为.MinimalnotSnoozable 也一样。
    • @Stephen:你知道提醒应用程序如何决定是使用“最小”上下文还是“默认”上下文的操作类别?
    • @Cchanchal - 通知类型取决于正在显示的通知。默认提醒是出现在主屏幕上的内容(我认为它支持 4 个操作)。当通知来自仅支持 2 个操作的通知横幅(顶部的下拉菜单/模态弹出通知)时,使用最小上下文。
    • @StephenDonnell,你说得对。我想知道 Reminder 应用程序如何默认使用“默认”上下文(当您安装 iOS 时),但默认情况下我们无法为我们的应用程序设置“默认”选项。用户将选择应用程序的通知应显示为警报还是横幅。我们不能强求。横幅是苹果为我们的应用选择的默认方式。
    【解决方案2】:

    我有一个不同的问题。我正在处理多种类型的通知。 我的通知中很少有没有显示操作按钮,而很少有。发现只有最后设置的通知显示操作按钮。当设置被共享时,我不认为类别标识符被最后设置的通知覆盖是愚蠢的。 为了解决这个问题,我们需要提取当前的通知设置,添加我们的设置并重新设置:

        NSMutableArray *arrNewCategories = [NSMutableArray new];
        UIUserNotificationSettings *oldSettings = [[UIApplication sharedApplication]currentUserNotificationSettings];
        for (UIMutableUserNotificationCategory *oldCategory in oldSettings.categories)
        {
          if (![oldCategory.identifier isEqualToString:newNotificationCategory.identifier])
               [arrNewCategories addObject:oldCategory];
        }
        [arrNewCategories addObject:newNotificationCategory];
    
        UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *newSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:[NSSet setWithArray:arrNewCategories]];
    
        [[UIApplication sharedApplication] registerUserNotificationSettings:newSettings];
    

    只需确保 newNotificationCategory 的标识符与您的 UILocalNotification 的类别匹配即可。

    【讨论】:

    • 我的本地通知也遇到了同样的问题,我应该把这段代码放在哪里,什么是 oldCategory,newNotificationCategory?
    • 无论您在何处创建通知,都应遵循此操作。要添加每个通知,您还需要添加一个 UIMutableUserNotificationCategory,这里对于我的新通知,它是带有新设置的 newNotificationCategory,循环中的 oldCategory 对象将包含您已经添加的所有先前通知的类别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多