在我的申请中,我需要在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说明吗?
请告诉我。
感谢您的回复,好吧,让我按照您的方式。 UILocalNotification是否可以启动所有设备,甚至我的部署从iOS 9.0开始?否则,我需要以单独的方式整合iOS 10.0的本地通知? – Ram
本地通知计划如下, {火灾日期= 2017年7月29日星期六下午6:00:00印度标准时间,时区=亚洲/加尔各答(GMT + 5:30)抵消19800 ,重复时间间隔= 0,重复次数= UILocalNotificationInfiniteRepeatCount,下一个火灾日期= 2017年7月29日星期六下午6:00:00印度标准时间,用户信息=(空)} 但我看不到本地通知警报。我在模拟器上测试过。 我们只能在设备上显示本地通知? –
Ram
本地通知在模拟器和设备中均有效。你应该在AppDelegate的didReceiveLocalNotification方法中检查它。 –