Interactive push notifications - Hide/Show buttons - ios

I followed this tutorial to display buttons on a 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 the buttons for different notifications ?

For adding another kind of interactive notification with no button you will have to update the 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 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 that the identifier of newNotificationCategory matches with your UILocalNotification's category, in which you dont 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];

I hope this is help 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 more swift
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)
}
Refer the Website

Related

Local Notification banner shows different text than what i set in alert action property

Here is my code to set local notifications for Event but it shows different text for different iphones like , for some iPhone it shows "Slide for more" and for some it shows "Touch to open" where as i set "view details" message:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.alertTitle = self.titleCell.propertyValue;
localNotification.alertBody = self.titleCell.propertyValue;
localNotification.alertAction = #"view details";
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"This local notification"
delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[notificationAlert show];//use latest alert action
}
yourControler.m
-(IBAction)startLocalNotification { // Bind this method to UIButton action
NSLog(#"startLocalNotification");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
notification.alertBody = #"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
you need register for local notification
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

how to repeat local notification after 14 days in iOS 8 & 9

The code to register for notifications is:
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
And the code to schedule the notification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
[self setNotificationTypesAllowed];
if (notification)
{
if (allowNotif)
{
notification.timeZone = [NSTimeZone defaultTimeZone];
if ( [statusString isEqualToString:#"daily"]) {
notification.fireDate = _timePick.date;
notification.repeatInterval = NSCalendarUnitDay;
}else if ( [statusString isEqualToString:#"weekly"]) {
notification.fireDate = _timePick.date;
notification.repeatInterval = NSCalendarUnitWeekOfYear;
}else if ( [statusString isEqualToString:#"fortnightly"]) {
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*14];;
notification.repeatInterval = NSCalendarUnitMinute;
//notification.repeatInterval = NSCalendarUnitYear;
}else{
notification.repeatInterval = 0;
}
}
if (allowsAlert)
{
notification.alertBody = [NSString stringWithFormat:#"Do you really want to send message to %#",name];
}
if (allowsBadge)
{
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
if (allowsSound)
{
notification.soundName = UILocalNotificationDefaultSoundName;
}
notification.alertAction = #"Yes";
notification.timeZone = [NSTimeZone defaultTimeZone];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showSMS) name:#"showSMS" object:nil];
// this will schedule the notification to fire at the fire date
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
I am able to repeat local notification daily & weekly but it is not repeating fortnightly ,please help
Yes as #Nikos suggested, try below code:
Cancel UILocalNotification.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Schedule local notification
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.userInfo = #{#"notification_identifier":#"After14Days"};
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(60*60*24*14)];
notification.alertBody = #"Text to display";
notification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
If you have any doubt then let me know.
To set a notification to fire every 14 days you have to create 26 separate UILocalNotifications with the repeatInterval set to NSYearCalendarUnit (the repeat interval must be set to a calendar unit, and there is no 14 days CalendarUnit in iOS).
Another way to handle it is to cancel the UILocalNotification in the didReceiveLocalNotification method and schedule a new one there for after 14 days. This method though assumes that the user interacts with the notification, otherwise the next one will never get scheduled.

iOS: notification actions - remove "open" in options menu

I've got notifications with some actions; now if there's only one action for a notification, it displays the "alert view" with a dismiss and an options button. When pressing the options, I get 'Open', 'My Action' and 'Close'.
How can I achieve to avoid the options menu and get 'Dismiss' and 'My Action' right from the beginning?
I'm talking about the alert that appears e.g. when you're on the iPhone home screen and you have selected alert style "Alerts" for "Alert Style When Unlocked"
I've got a local push notification with just one action defined. Now I would expect to get the notification on the Home Screen with "Close" on the left and my defined action on the right. But instead I get this one:
I would love to avoid the additional step of tapping "Options" and then only get one real option.
The code I'm using:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) {
return;
}
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:NOTIFICATION_DELAY];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = text;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.category = ERROR_CATEGORY;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
registering for push notifications:
UIMutableUserNotificationAction *callSupportAction = [self actionWithIdentifier:#"callSupportAction" title:NSLocalizedString(#"callSupportActionTitle", #"") background:YES destructive:NO authenticationRequired:NO];
UIMutableUserNotificationCategory *errorCategory = [self categoryWithIdentifier:ERROR_CATEGORY actions:#[callSupportAction]];
NSSet *categories = [NSSet setWithObjects:errorCategory, nil];
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
- (UIMutableUserNotificationAction *)actionWithIdentifier:(NSString *)identifier title:(NSString *)title background:(BOOL)background destructive:(BOOL)destructive authenticationRequired:(BOOL)authenticationRequired {
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.identifier = identifier;
action.title = title;
action.activationMode = background ? UIUserNotificationActivationModeBackground : UIUserNotificationActivationModeForeground;
action.destructive = destructive;
action.authenticationRequired = authenticationRequired;
return action;
}
- (UIMutableUserNotificationCategory *)categoryWithIdentifier:(NSString *)identifier actions:(NSArray *)actions {
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = identifier;
if (actions.count > 2) {
[category setActions:actions forContext:UIUserNotificationActionContextDefault];
[category setActions:[actions subarrayWithRange:NSMakeRange(0, 2)] forContext:UIUserNotificationActionContextMinimal];
} else {
[category setActions:actions forContext:UIUserNotificationActionContextDefault];
}
return category;
}

How to create local notification ios

UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
I'm not receiving local notification,can anyone say how to get local notification.
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
Try this!!!!
You need to specify the time when to fire notification. You have just created notification but not set when it need to be fired.
For example..
localNotif.fireDate = [itemDate dateByAddingTimeIntervalInterval:-(minutesBefore*60)];
If application is foreground stage then you wan't seen those notification. you should have to code on delegate method.
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
UILocalNotification *notif = [[UILocalNotification alloc]init];
notif.alertBody = #"Your Session is Ready";
notif.alertAction = #"Take Now";
notif.soundName = #"DingBell.wav";
[notif setFireDate:[gregCalendar dateFromComponents:dateComponent]];
notif.repeatInterval = NSWeekCalendarUnit;
[notif setTimeZone:[NSTimeZone defaultTimeZone]];
NSMutableDictionary *userDict = [NSMutableDictionary dictionary];
[userDict setValue:self.stringModuleID forKey:#"kRemindMeNotificationDataKey"];
[userDict setValue:[NSString stringWithFormat:#"alarm%d",[[setreminingDay objectAtIndex:i] intValue]] forKey:#"alarm"];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];

Local Notification is firing, but not visible. (iOS)

I am using the following code to make a local notification:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:30]; //itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = parentController.activeAssignment.notes;
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
I have checked using the method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
that the notification is firing, but is not showing up on my screen when the app is running in the background.

Resources