如何为特定日期设置本地通知,时间为中午 12 点和下午 6 点
How to set local notification for only particular date with time 12PM and 6PM
在我的应用程序中,我需要在中午 12 点和下午 6 点的特定日期触发虚假通知。我已经在“UILocalNotification”类别文件中创建并安排了本地通知。
UILocalNotification+TZT.h 文件:
#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 文件:
#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];
}
在 Appdelegate class didFinishLaunchingWithOptions 方法中,我必须通过调用类别 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)];
}
例如,7月31日中午12点和下午6点我想显示本地通知,所以我将静态日期设置为字符串然后转换为NSDate,在我创建NSArray后包含NSTimeInterVal值。
中午 12 点为 43200 秒,下午 6 点为 64800 秒,使用这些时间间隔值安排。
但是我也看不到本地通知模拟器和设备。
应用部署目标从iOS9.0
开始
所以我需要管理 iOS 9.0 及以下和 iOS 10. 及以上情况的本地通知?
我还需要遵循 iOS-10 说明吗?
请给我建议。
这不是触发通知的正确方法。使用 NSCalendar 获取日期组件。这是为您想要的日期和时间触发通知的功能。
-(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];
}
像这样调用上面的方法。我使用 DateComponent 在 2 天后获取日期。你可以传递你的约会对象。
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"];
这是通知的 FireDate 的输出。中午 12 点和下午 6 点通知。
2017-07-31 06:00:00 +0530
2017-07-31 12:00:00 +0530
在我的应用程序中,我需要在中午 12 点和下午 6 点的特定日期触发虚假通知。我已经在“UILocalNotification”类别文件中创建并安排了本地通知。
UILocalNotification+TZT.h 文件:
#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 文件:
#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];
}
在 Appdelegate class didFinishLaunchingWithOptions 方法中,我必须通过调用类别 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)];
}
例如,7月31日中午12点和下午6点我想显示本地通知,所以我将静态日期设置为字符串然后转换为NSDate,在我创建NSArray后包含NSTimeInterVal值。
中午 12 点为 43200 秒,下午 6 点为 64800 秒,使用这些时间间隔值安排。
但是我也看不到本地通知模拟器和设备。
应用部署目标从iOS9.0
开始所以我需要管理 iOS 9.0 及以下和 iOS 10. 及以上情况的本地通知?
我还需要遵循 iOS-10 说明吗?
请给我建议。
这不是触发通知的正确方法。使用 NSCalendar 获取日期组件。这是为您想要的日期和时间触发通知的功能。
-(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];
}
像这样调用上面的方法。我使用 DateComponent 在 2 天后获取日期。你可以传递你的约会对象。
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"];
这是通知的 FireDate 的输出。中午 12 点和下午 6 点通知。
2017-07-31 06:00:00 +0530
2017-07-31 12:00:00 +0530