IOS:handleActionWithIdentifier事件未在真实设备上触发

时间:2016-01-28 05:42:48

标签: ios objective-c uilocalnotification

我正在使用UILocalNitification在应用程序在后台运行时发送通知,我的代码是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    // ask fo permission to send alert
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil]];
    }

    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 = @"Take Photos";

    // 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 = @"PHOTO";

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

    [self.window makeKeyAndVisible];
    return YES;
}

用于处理按钮事件的代码是:

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

    if ([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]) {
        NSLog(@"Here");

    }

    completionHandler();
}

我的问题是在模拟器上进行测试时,可以捕获按钮事件,但在真实设备(iOS版本为8.1.2和iPhone 5S iOS8.1.X的iPod)上进行测试时,两个设备都无法获取按钮事件

有人可以帮忙吗?

0 个答案:

没有答案