iPhone Local Notifications Won't Appear [dupli

2019-07-31 20:44发布

Possible Duplicate:
UILocalNotification isn't working at all

I'm writing an app that sends the user an alert through the Notification Center when an event date is approaching. But when I set the date in the date picker and close the app, the notification doesn't appear. I already enabled Push Notifications in my provisioning profiles. This is all the code in my project that deals with the notification center,This is all the code in my view controller file dealing with the date picker:

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

}

And this is everything in my App delegate dealing with local notifications:

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

Any help is much appreciated, thank you!

1条回答
可以哭但决不认输i
2楼-- · 2019-07-31 21:16

following code is use for the local notification.

-(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];
}
查看更多
登录 后发表回答