IOS custom buttons not showing for interactive notification
I am currently working on live iOS notifications made by 8, and to be honest, I am really trying to see my buttons. With the code below, I can't see my buttons:
// INTERACIVE
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];
Any idea for my problem? Despite all the examples on the internet, I am apparently the only one with this problem.
source to share
Here's a really great tutorial: Ninja Code
Everything in your code looks great. I have to ask you not to just ignore what's actually going on - if you're looking at a notification on your home screen, did you skip the notification to the left to reveal the buttons?
This is what I am using (sorry for the quick)
Initial prompt for notifications
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])
))
Local notification
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)
source to share
I had another problem. I have dealt with several types of notifications. Few of my notifications didn't show action buttons, and some did . It turned out that only the notification that was installed last showed action buttons. I was stupid not to think that the category ID is being overridden by the latest set notification when sharing settings. To deal with this, we need to extract the current notification settings, add them to it, and set it again:
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];
Just make sure the newNotificationCategory ID matches your UILocalNotification category.
source to share