iPhone本地通知将不会出现

时间:2012-01-22 04:17:44

标签: iphone ios xcode notifications local

  

可能重复:
  UILocalNotification isn't working at all

我正在编写一个应用程序,当事件日期即将来临时,通过通知中心向用户发送提醒。但是,当我在日期选择器中设置日期并关闭应用程序时,通知不会出现。我已在配置文件中启用了推送通知。这是我的项目中处理通知中心的所有代码,这是我的视图控制器文件中处理日期选择器的所有代码:

- (IBAction)dateChanged:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSDate *selectedDate = [self.datePicker date];

    [defaults setObject:selectedDate forKey:@"ImportantDatesViewController.selectedDate"];
    [defaults synchronize];

}

- (void)viewDidLoad {
    NSDate *storedDate = [[NSUserDefaults standardUserDefaults] 
                          objectForKey:@"ImportantDatesViewController.selectedDate"];
    if (storedDate == nil) {
        storedDate = [NSDate date];
    }

    [self.datePicker setDate:storedDate animated:NO];

}

这就是我的App委托处理本地通知的所有内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
....

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

    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:-13*60*60];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];


    localNotif.alertBody = @"Event in three days!";

    localNotif.alertAction = nil;

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];    

    return YES;  

}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString* pushToken = [[[[deviceToken description] 
                             stringByReplacingOccurrencesOfString:@"<"withString:@""] 
                            stringByReplacingOccurrencesOfString:@">" withString:@""] 
                           stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSLog(@"%@", pushToken);

}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {

    NSLog(@"error: %@", error);
}

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

1 个答案:

答案 0 :(得分:2)

以下代码用于本地通知。

-(IBAction)buttonPressed:(UIButton *)button
{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    if (!localNotification)
        return;

    // Current date
    NSDate *date = [NSDate date]; 

    // Add one minute to the current time
    NSDate *dateToFire = [date dateByAddingTimeInterval:20];

    // Set the fire date/time
    [localNotification setFireDate:dateToFire];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

    // Create a payload to go along with the notification
    NSArray *array = [NSArray arrayWithObjects:@"Value 1", @"Value 2", nil];
    NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"];
    [localNotification setUserInfo:data];

    if (button == buttonAlert || button == buttonAll)
    {
        // Setup alert notification
        [localNotification setAlertBody:@"Incoming Local Notification" ];
        [localNotification setAlertAction:@"Open App"];
        [localNotification setHasAction:YES];
    }

    if (button == buttonBadge || button == buttonAll)
    {
        // Set badge notification, increment current badge value
        [localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1];
    }

    if (button == buttonSound || button == buttonAll)
    {
        // Setup sound notification
        [localNotification setSoundName:UILocalNotificationDefaultSoundName];
    }

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}