0

我正在编写一个应用程序,当事件日期临近时,它会通过通知中心向用户发送警报。但是当我在日期选择器中设置日期并关闭应用程序时,通知不会出现。我已经在我的配置文件中启用了推送通知。可能是因为我的 objectForKey 区域。我有 2 个笔尖(一个用于 iPhone,一个用于 iPad),分别命名为 ImportDatesViewController_iPhone1 和 ImportantDatesViewController_iPad1。我应该将 objectForKey 区域更改为笔尖名称,而不仅仅是“ImportantDatesViewController”吗?而且我的 .h 和 .m 文件名也只是 ImportDatesViewController。对不起,我在这方面还是很新的,边走边学。这是我的项目中处理通知中心的所有代码,这就是我放在视图控制器中的代码:

- (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;

}

我还在底部 App Delegate 的 didFinishLaunchingWithOptions 方法中添加了这段代码,我认为它可以解决问题:

[[UIApplication sharedApplication]registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];

非常感谢任何帮助,谢谢!

4

3 回答 3

4

Your telling it to fire a notification immediately when you set it and your setting it for a time before now by setting (NOW)-60*60*60.The notification has passed already.

[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];  

if you want to set it for a specific time:

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

If the specified value is nil or is a date in the past, the notification is delivered immediately. As a side note make sure to set the timeZone and Locale to the ones you want.

于 2012-01-22T02:03:26.617 回答
1
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];
于 2012-07-31T10:59:45.017 回答
1

这是适用于我的项目的 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,并带有默认通知声音。

于 2014-03-26T11:44:55.643 回答