iOS 8 通知操作按钮不显示

标签 ios button uilocalnotification

我正在尝试使用操作按钮发送本地通知。 通知没问题。但不显示操作按钮。 我正在使用 Xcode 6.3.2 iPhone 6 iOS 8.2


发送通知

UIApplication *myapp = [UIApplication sharedApplication];
UILocalNotification *test = [[UILocalNotification alloc] init];

if(test) {
    NSLog(@"Sending notification");

    test.alertBody = @"jothi test alert";
    test.alertTitle = @"kottapa";

    test.soundName = UILocalNotificationDefaultSoundName;
    test.alertAction = NSLocalizedString(@"View Details", nil);
    test.category = @"MY_CATEGORY";

    [myapp presentLocalNotificationNow:test];
}

在应用程序委托(delegate)中didFinishLaunchingWithOptions:

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

myAction.identifier = @"MY_IDENTIFIER";        
myAction.title = @"Test Action";
myAction.activationMode = UIUserNotificationActivationModeBackground;
myAction.destructive = NO;
myAction.authenticationRequired = NO;

//------------

// First create the category
UIMutableUserNotificationCategory *inviteCategory =
   [[UIMutableUserNotificationCategory alloc] init];

// Identifier to include in your push payload and local notification
inviteCategory.identifier = @"MY_CATEGORY";

// Add the actions to the category and set the action context
[inviteCategory setActions:@[myAction]
                forContext:UIUserNotificationActionContextDefault];

// Set the actions to present in a minimal context
[inviteCategory setActions:@[myAction]
                forContext:UIUserNotificationActionContextMinimal];

NSSet *categories = [NSSet setWithObject:inviteCategory];

UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound
                                      categories:categories];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

enter image description here

最佳答案

您的代码有效,您只需向左滑动通知即可看到操作按钮(或者在设备未锁定时将其下拉,如果它以顶部横幅的形式呈现) )。

另外,我猜您已经这样做了,但只是没有包含代码,为了处理用户选择的操作,您需要在应用程序委托(delegate)中实现此方法:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler

enter image description here

关于iOS 8 通知操作按钮不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32109729/

相关文章:

ios - 不包含 GvrViewerMain 的 Google Cardboard SDK

css - 定位上一个和下一个按钮(在 NivoSlider 上)

ios - 如何在iOS8中将通知类型从横幅更改为警报?

ios - 在运行时隐藏通知操作按钮

javascript - Android 上 jQuery 脚本中的错误

objective-c - didSelectRowAtIndexPath UIButton

iphone - iPhone SDK 中的透明按钮

iphone - 从后台注册本地通知

ios - 对容器 View 使用分段控件时出现错误代码

android - 如何对齐按钮中的文本