Interactive push notifications - Hide / Show buttons
I followed this tutorial to display buttons in push notification.
Buttons are registered by calling registerNotification in didFinishLaunchingWithOptions
.
However, in few cases, I need to display a simple notification without any buttons. How to show / hide buttons for different notifications?
source to share
To add another type of interactive notification without a button, you will need to update UIUserNotificationSettings
.
Create a new notification category UIMutableUserNotificationCategory
without any button:
UIMutableUserNotificationCategory *newNotificationCategory = [[UIMutableUserNotificationCategory alloc] init];
newNotificationCategory.identifier = @"no_button_id";
Then add this new category to the existing one UIUserNotificationSettings
:
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 where you don't need any buttons.
Then schedule the notification:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:afireDate];
localNotification.alertBody = @"alert body text";
localNotification.category = @"no_button_id"; // Same as category identifier
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.soundName = SOUND_FILE;
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
source to share
Hope this helps you.
Parameter Description. userInfo -> dictionary value for user needs. title -> notitification Title. fireDate -> trigger notification date and time. Interactive -> pass flag to set Interactive or normal notification.
-(void)setLocalnotfication:(NSDictionary *)userInfo title:(NSString *)title date:(NSDate *)fireDate Interactive : (BOOL)flag {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.alertBody = title;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
if(flag) notification.category = @"Category name";
if([userInfo isKindOfClass:[NSDictionary class]]) notification.userInfo = userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
I am using this code to create it.
and faster
func setLocalnotfication(userInfo: [NSObject : AnyObject], title: String, date fireDate: NSDate, Interactive flag: Bool) {
var notification: UILocalNotification = UILocalNotification()
notification.fireDate = fireDate
notification.alertBody = title
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.soundName = UILocalNotificationDefaultSoundName
if flag {
notification.category = NCI
}
if (userInfo is NSDictionary.self) {
notification.userInfo = userInfo
}
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
source to share