我已经以编程方式添加了本地通知,如下所示:
UILocalNotification *eventLocalNotification=[[UILocalNotification alloc]init];
eventLocalNotification.fireDate=myDate;
eventLocalNotification.timeZone=[NSTimeZone defaultTimeZone];
eventLocalNotification.alertBody=@"My notification";
eventLocalNotification.soundName=UILocalNotificationDefaultSoundName;
我可以更改firingDate,timeZone,alertBody或任何其他属性吗?
答案 0 :(得分:2)
经过互联网搜索后,我得到了一个我们不能edit
添加UILocal通知的东西。但确定我找到了另一种方式。
以下是添加通知的方法。
-(void)setLocalNotification
{
UILocalNotification *eventLocalNotification = [[UILocalNotification alloc]init];
eventLocalNotification.fireDate = //set firing Date of NSDate type
eventLocalNotification.timeZone = [NSTimeZone defaultTimeZone];
eventLocalNotification.alertBody = [NSString stringWithFormat:@"An event has arrived\n Event Name: %@",notificationName.Text];
eventLocalNotification.soundName = UILocalNotificationDefaultSoundName;
if ([repeat isEqualToString:@"Once"]){
eventLocalNotification.repeatInterval = 0;
}else if ([repeat isEqualToString:@"Daily"]){
eventLocalNotification.repeatInterval = NSDayCalendarUnit;
}else if ([repeat isEqualToString:@"Weekly"]){
eventLocalNotification.repeatInterval = NSWeekCalendarUnit;
}else if ([repeat isEqualToString:@"Monthly"]){
eventLocalNotification.repeatInterval = NSMonthCalendarUnit;
}else if ([repeat isEqualToString:@"Yearly"]){
eventLocalNotification.repeatInterval = NSYearCalendarUnit;
}
NSDictionary *info = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%@",notificationName.text] forKey:@"name"];
eventLocalNotification.userInfo = info;
NSLog(@"notification userInfo gets name : %@",[info objectForKey:@"name"]);
[[UIApplication sharedApplication] scheduleLocalNotification:eventLocalNotification];
NSLog(@"Notification created");
}
以下是取消通知的功能
-(void)cancelLocalNotification
{
UILocalNotification * notificationToCancel=nil;
for(UILocalNotification * aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications])
{
NSLog(@"%@",[aNotif.userInfo objectForKey:@"name"]);
if([[aNotif.userInfo objectForKey:@"name"] isEqualToString:notificationName ])
{
notificationToCancel = aNotif;
[[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];
NSLog(@"Notification Cancelled");
break;
}
}
}
希望你能从中受益。最好的运气
答案 1 :(得分:1)
要设置日期和时间,您必须使用NSDateComponents并实例化两次NSDate。一个用于当前日期,另一个用于您理想的日期。然后将理想的NSDate实例设置为UILocalNotification对象的fireDate。
eventLocalNotification.fireDate = desiredDate;
对于时区,请按照您的方式保持默认值。
eventLocalNotification.timeZone = [NSTimeZone defaultTimeZone];
警报正文可以是任何东西,只需记下你的背景。
eventLocalNotification.alertBody = @"DESIRED CONTEXT";