2017-03-10 4 views
0

通知を表示する次のコードがあります。それは作業ですが、アクションボタンは表示されません。何が間違っているのだろうか?UNNotificationActionがアクションを表示しない

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 


    UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound; 

    [center requestAuthorizationWithOptions:options 
          completionHandler:^(BOOL granted, NSError * _Nullable error) { 
           if (!granted) { 
            NSLog(@"Something went wrong"); 
           } 
          }]; 

    // Objective-C 
    UNMutableNotificationContent *content = [UNMutableNotificationContent new]; 
    content.title = @"Don't forget"; 
    content.body = @"Buy some milk"; 
    content.sound = [UNNotificationSound defaultSound]; 

    // Time 
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:30 
                            repeats:NO]; 

    // Objective-C 
    NSString *identifier = @"UYLLocalNotification"; 
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier 
                      content:content trigger:trigger]; 

    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
     if (error != nil) { 
      NSLog(@"Something went wrong: %@",error); 
     } 
    }]; 


    // Actions 
    UNNotificationAction *snoozeAction = [UNNotificationAction actionWithIdentifier:@"Snooze" 
                       title:@"Snooze" options:UNNotificationActionOptionNone]; 
    UNNotificationAction *deleteAction = [UNNotificationAction actionWithIdentifier:@"Delete" 
                       title:@"Delete" options:UNNotificationActionOptionDestructive]; 

    // Objective-C 
    UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"UYLReminderCategory" 
                       actions:@[snoozeAction,deleteAction] intentIdentifiers:@[] 
                       options:UNNotificationCategoryOptionNone]; 
    NSSet *categories = [NSSet setWithObject:category]; 

    // Objective-C 
    [center setNotificationCategories:categories]; 

    // Objective-C 
    content.categoryIdentifier = @"UYLReminderCategory"; 
+0

「センターaddNotificationRequest ..」がコードからの最後の呼び出しになるようにアクションを設定した後で、通知を追加しようとします。 – JackRobinson

答えて

0

あなたは完全にそのnotificationCategories & categoryIdentifierを設定することで、あなたのUNMutableNotificationContentを設定する前あなたはUNNotificationRequestを作成しました。

ちょうどあなたの問題を解決する必要があり、コードの末尾に次の行を移動:

NSString *identifier = @"UYLLocalNotification"; 
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier 
                     content:content trigger:trigger]; 

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
    if (error != nil) { 
     NSLog(@"Something went wrong: %@",error); 
    } 
}]; 

、シミュレータは、3Dタッチをサポートしていないので、実際のデバイス上でそれをテストする方が良いだろう。通知が表示されたら、それに触れると、アクションボタンが表示されます。

関連する問題