简体   繁体   中英

iOS - Local Notification in Background

I have a problem, after setting up the notification in the file ViewController.m

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSDateComponents *componentsForReferenceDate = [calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];

    [componentsForReferenceDate setDay: 9];
    [componentsForReferenceDate setMonth: 11];
    [componentsForReferenceDate setYear: 2012];

    NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];

    // IMPOSTO ORA NOTIFICA

    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: referenceDate];

    [componentsForFireDate setHour: 19];
    [componentsForFireDate setMinute: 13];
    [componentsForFireDate setSecond: 00];

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];

    // CREO LA NOTIFICA

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

    notification.fireDate = fireDateOfNotification;
    notification.timeZone = [NSTimeZone localTimeZone];
    notification.alertBody = [NSString stringWithFormat: @"Get inspired. A new quote is available."];
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some Information"] forKey:@"Quotes"];
    notification.repeatInterval= NSDayCalendarUnit;
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.applicationIconBadgeNumber = 1;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

In AppDelegate.m , I want to run a method when the user opens the notification and the app is in the background.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    //SE L'APP VIENE LANCIATA TRAMITE LA NOTIFICA
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if (notification)
    {
        [self.viewController generaCitazione];
    }

    return YES;
}

where am I wrong?

You have used the wrong key "UIApplicationLaunchOptionsRemoteNotificationKey". This key is for push notifications. Use the following key for Local notifs.

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM