且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iPhone本地通知不会出现

更新时间:2023-02-26 20:57:15

以下代码用于本地通知.

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