iOS中的本地通知无法正常工作

时间:2016-05-31 11:13:44

标签: ios uilocalnotification

我是iOS编程的新手,我正在尝试简单的本地通知,当用户按下按钮我正在设置日期和时间通知时,通知不会触发。请回复

code:

    - (IBAction)sendNotification:(id)sender
    {


        int hour=4;
        int minute=8;
        int month=5;
        int year=2016;
        int day=31;


        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setYear:year];
        [components setMonth:month];
        [components setDay:day];
        [components setHour: hour];
        [components setMinute: minute];
        [components setSecond: 00];
         [components setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
        NSDate * date=[calendar dateFromComponents:components];
        [self scheduleNotificationForData:date];
    }



    -(void)scheduleNotificationForData:(NSDate *)date
    {

        UILocalNotification *localNotification = [[UILocalNotification alloc] init];

        localNotification.fireDate = date;
        NSLog(@"Notification will be shown on: %@",localNotification.fireDate);

        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.alertBody = [NSString stringWithFormat:@"Your notification message"];
        localNotification.alertAction = NSLocalizedString(@"View details", nil);

        /* Here we set notification sound and badge on the app's icon "-1"
         means that number indicator on the badge will be decreased by one
         - so there will be no badge on the icon */
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.applicationIconBadgeNumber = -1;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];


    }

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    UILocalNotification * localNotification=[[UILocalNotification alloc]init];
    [localNotification setAlertBody:@"this is notification"];
    [localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
    [application setScheduledLocalNotifications:[NSArray arrayWithObjects:localNotification, nil]];


}

0 个答案:

没有答案