0

在我的申请中,我需要在12PM和6PM的特定日期触发虚假通知。我在“UILocalNotification”类别文件中创建并安排了本地通知。如何为时间12PM和6PM的特定日期设置本地通知

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类didFinishLaunchingWithOptions方法,我要创建和计划通过调用类别类的本地通知。

#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值。

12PM作为43200秒,也是6PM作为64800秒计划与这些时间间隔值。

但我看不到本地通知模拟器以及设备也。

应用部署目标从9.0的iOS开始

所以我需要管理本地通知适用于iOS 9.0及以下和iOS 10.与上述情况?

我需要关注iOS-10说明吗?

请告诉我。

回答

0

这不是正确的方法来触发通知。使用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 PM & 6 PM通知。

2017-07-31 06:00:00 +0530 
2017-07-31 12:00:00 +0530 
+0

感谢您的回复,好吧,让我按照您的方式。 UILocalNotification是否可以启动所有设备,甚至我的部署从iOS 9.0开始?否则,我需要以单独的方式整合iOS 10.0的本地通知? – Ram

+0

本地通知计划如下, {火灾日期= 2017年7月29日星期六下午6:00:00印度标准时间,时区=亚洲/加尔各答(GMT + 5:30)抵消19800 ,重复时间间隔= 0,重复次数= UILocalNotificationInfiniteRepeatCount,下一个火灾日期= 2017年7月29日星期六下午6:00:00印度标准时间,用户信息=(空)} 但我看不到本地通知警报。我在模拟器上测试过。 我们只能在设备上显示本地通知? – Ram

+0

本地通知在模拟器和设备中均有效。你应该在AppDelegate的didReceiveLocalNotification方法中检查它。 –

相关问题