簡體   English   中英

iOS-后台本地通知

[英]iOS - Local Notification in Background

在文件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];

AppDelegate.m中 ,我想在用戶打開通知且應用程序在后台時運行一個方法。

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

我哪里錯了?

您使用了錯誤的鍵“ UIApplicationLaunchOptionsRemoteNotificationKey”。 該鍵用於推送通知。 將以下鍵用於本地通知。

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM