Reputation: 804
In my application, I need to trigger a fake notification on a particular date with time 12PM and also 6PM. I have created and scheduled local notification in "UILocalNotification" category file.
UILocalNotification+TZT.h file :
#import <UIKit/UIKit.h>
@interface UILocalNotification (TZT)
//Create LocalNotification
+ (void)createLocalNotificaitonForDateForDate:(NSDate *)date
withBody:(NSString *)body
withUserInfo:(NSDictionary *)userInfo
withTimeInterValInfo:(NSArray *)timeInterValInfo
withBtnTitle:(NSString *)btnTitle;
//Cancel All Local notificaitons
+ (void)cancelAllLocalNotifications;
@end
UILocalNotification+TZT.m file :
#import "UILocalNotification+TZT.h"
@implementation UILocalNotification (TZT)
+ (void)createLocalNotificaitonForDateForDate:(NSDate *)date
withBody:(NSString *)body
withUserInfo:(NSDictionary *)userInfo
withTimeInterValInfo:(NSArray *)timeInterValInfo
withBtnTitle:(NSString *)btnTitle
{
UILocalNotification * localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = body;
localNotification.alertAction = btnTitle;
localNotification.userInfo = userInfo;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = 0;
localNotification.soundName = UILocalNotificationDefaultSoundName;
for (int i = 0; i < [timeInterValInfo count]; i++) {
NSLog(@"timeInterValInfo = %ld",(long)[[timeInterValInfo objectAtIndex:i] integerValue]);
date = [date dateByAddingTimeInterval:(long)[[timeInterValInfo objectAtIndex:i] integerValue]];
localNotification.fireDate = date;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
//localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
+ (void)cancelAllLocalNotifications
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
In Appdelegate class didFinishLaunchingWithOptions method, I have to create and scheduled local notification by calling category class.
#pragma mark - UILocalNotification
- (void)setUpLocalNotification
{
NSDate * firedDate = [TZTNSDateHandlings getDateMonthYearHyPhenFormat:@"31-07-2017 00:00:00" withHourMinSec:YES];
NSArray * timeInterValArray = @[@"43200",@"64800"];
NSDictionary * userInfoDic = @{@"receiverType":@"localNotification"
};
[UILocalNotification createLocalNotificaitonForDateForDate:firedDate
withBody:@"Sample local notification"
withUserInfo:userInfoDic
withTimeInterValInfo:timeInterValArray
withBtnTitle:NSLocalizedString(@"localPush.btnTitle", nil)];
}
For example, On 31st July 12PM and 6PM I want show the local notification, So I set static date as a string then convert to NSDate, after I have created NSArray contains NSTimeInterVal values.
12PM as a 43200 seconds and also 6PM as a 64800 seconds scheduled with these time interval values.
But I can't see the Local notification Simulator as well as Device also.
Application deployment target starts from iOS 9.0
So I need to manage local notification for iOS 9.0 and below and iOS 10. and above cases ?
I need to follow iOS-10 instructions also ?
Kindly suggest me.
Upvotes: 0
Views: 797
Reputation: 427
This is not proper way to fire notification. Use NSCalendar for getting Date Components. This is the function that fire Notification for your desired Date and Time.
-(void)fireNotificationonDate:(NSDate *)dateEnter onTime:(NSString *)strTime
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *timeComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:dateEnter];
[timeComponents setHour:[strTime integerValue]];
[timeComponents setMinute:00];
[timeComponents setSecond:0];
[timeComponents setYear:[timeComponents year]];
[timeComponents setMonth:[timeComponents month]];
[timeComponents setDay:[timeComponents day]];
NSDate *dtFinal = [calendar dateFromComponents:timeComponents];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSString *fierDate = [formatter stringFromDate:dtFinal];
NSLog(@"%@",fierDate);
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = dtFinal;
notification.alertBody = @"";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Call above method like this. I use DateComponent for getting date after 2 days. You can pass your date.
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
comps.day = 2;
NSDate *twoDaysAfter = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
[self fireNotificationonDate:twoDaysAfter onTime:@"6"];
[self fireNotificationonDate:twoDaysAfter onTime:@"12"];
This is Output for FireDate of Notification. 12 PM & 6 PM notification.
2017-07-31 06:00:00 +0530
2017-07-31 12:00:00 +0530
Upvotes: 0