Xcode iphoneios:本地通知不是';t出现
我正在编写一个应用程序,在事件日期临近时通过通知中心向用户发送警报。但当我在日期选择器中设置日期并关闭应用程序时,通知不会出现。我已经在我的配置文件中启用了推送通知。这可能是因为我的objectForKey区域。我有两个笔尖(一个用于iPhone,一个用于iPad),分别命名为ImportantDatesViewController_iPhone1和ImportantDatesViewController_iPad1。我是否应该将objectForKey区域更改为nib名称,而不仅仅是“ImportantDatesViewController”?而我的.h和.m文件名也很重要。对不起,我在这方面还是很新的,我会边学边学。 以下是我的项目中处理通知中心的所有代码,这是我在视图控制器中放置的代码:Xcode iphoneios:本地通知不是';t出现,xcode,datepicker,notifications,uilocalnotification,localnotification,Xcode,Datepicker,Notifications,Uilocalnotification,Localnotification,我正在编写一个应用程序,在事件日期临近时通过通知中心向用户发送警报。但当我在日期选择器中设置日期并关闭应用程序时,通知不会出现。我已经在我的配置文件中启用了推送通知。这可能是因为我的objectForKey区域。我有两个笔尖(一个用于iPhone,一个用于iPad),分别命名为ImportantDatesViewController_iPhone1和ImportantDatesViewController_iPad1。我是否应该将objectForKey区域更改为nib名称,而不仅仅是“Impo
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm'/'dd'/'yyyy"];
NSDate *eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"ImportantDatesViewController.selectedDate"];
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-60*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Event coming in three days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
return YES;
}
我还在底部的应用程序委托中的didFinishLaunchingWithOptions方法中添加了这段代码,我认为这会起到作用:
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
非常感谢您的帮助,谢谢 当您设置通知时,告诉它立即启动通知,并通过设置(now)-60*60*60来设置一段时间。通知已通过
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
如果要将其设置为特定时间:
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
如果指定的值为零或是过去的日期,则会立即发送通知。
作为旁注,请确保将时区和地区设置为所需的时区和地区。您告诉它在设置时立即触发通知,并通过设置(now)-60*60*60将其设置为之前的一段时间。通知已通过
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
UILocalNotifications *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] dateByAddingTimeInterval:900] ; //15 min
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Parking your CAR 15 Minutes reached..";
// localNotif.repeatInterval=kCFCalendarUnitMinute;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
如果要将其设置为特定时间:
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
如果指定的值为零或是过去的日期,则会立即发送通知。
作为旁注,请确保将时区和区域设置为所需的时区和区域设置。以下是适用于我的项目的LocalNotification示例代码
UILocalNotifications *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] dateByAddingTimeInterval:900] ; //15 min
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Parking your CAR 15 Minutes reached..";
// localNotif.repeatInterval=kCFCalendarUnitMinute;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
AppDelegate文件中的此代码块:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
// Override point for customization after application launch.
return YES;
}
// This code block is invoked when application is in foreground (active-mode)
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"This local notification"
delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[notificationAlert show];
// NSLog(@"didReceiveLocalNotification");
}
此代码块位于任何ViewController的.m文件中:
-(IBAction)startLocalNotification { // Bind this method to UIButton action
NSLog(@"startLocalNotification");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
notification.alertBody = @"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
当按下绑定“startLocalNotification”的按钮时,上述代码在7秒的时间间隔后显示AlertView
若应用程序位于后台,则它会将BadgeNumber显示为10,并带有默认通知声音。以下是为我的项目工作的LocalNotification的示例代码 AppDelegate文件中的此代码块:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
// Override point for customization after application launch.
return YES;
}
// This code block is invoked when application is in foreground (active-mode)
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"This local notification"
delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[notificationAlert show];
// NSLog(@"didReceiveLocalNotification");
}
此代码块位于任何ViewController的.m文件中:
-(IBAction)startLocalNotification { // Bind this method to UIButton action
NSLog(@"startLocalNotification");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
notification.alertBody = @"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
当按下绑定“startLocalNotification”的按钮时,上述代码在7秒的时间间隔后显示AlertView
如果应用程序位于后台,则它会将BadgeNumber显示为10,并带有默认通知声音。只是一个旁注,我以前发布过,但发布不正确,我删除了以前的一个,并以正确的格式发布。可能只是旁注的重复,我以前发布过,但发布不正确,我删除了前一个,并以正确的格式发布了这篇文章。可能是重复的,我从来没有真正检查过,直到你提到它,但我的出现在那里。如果你在一台设备上进行测试,我看不出有任何理由不这样做。哦,还有一件事,那么-60*60*60是在用户设置的日期选择器到达日期前60小时发送通知的正确公式吗?我不能确定,因为我经常使用NSDateComponents,只会将时间设置为-60。但你的解决方案看起来也很可靠,我不能100%肯定。有时候,做同一件事的方法不止一种。啊,糟了,实际上它不起作用,对不起,我弄错了。我将其更改为
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif]代码>但这似乎没有什么不同。如果设备处于睡眠模式且应用程序未运行,它是否仍能工作?我还编辑了更多的细节。我从来没有真正检查过,直到你提到它,但我的出现在那里。如果你在一台设备上进行测试,我看不出有任何理由不这样做。哦,还有一件事,那么-60*60*60是在用户设置的日期选择器到达日期前60小时发送通知的正确公式吗?我不能确定,因为我经常使用NSDateComponents,只会将时间设置为-60。但你的解决方案看起来也很可靠,我不能100%肯定。有时候,做同一件事的方法不止一种。啊,糟了,实际上它不起作用,对不起,我弄错了。我将其更改为[[UIApplication sharedApplication]scheduleLocalNotification:localNotif]代码>但这似乎没有什么不同。如果设备处于睡眠模式且应用程序未运行,它是否仍能工作?我还编辑了带有更多细节的OP。