iphone - 如何判断收到了哪个本地通知? Objective-C

标签 iphone objective-c ios uilocalnotification

我有这段代码根据收到的本地通知调用不同的 NSLog 语句:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    if (notification == automaticBackupNotification)
    {
        NSLog(@"Backup notification received.");
    }
    else
    {
        NSLog(@"Did receive notification: %@, set for date:%@ .", notification.alertBody, notification.fireDate);
    }
}

然后我使用这个方法在另一个类中安排通知:

- (IBAction)automaticValueChanged {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (automaticSwitch.isOn){
        [defaults setValue:@"1" forKey:@"automatic"];
        //schedule notification
        //Set up the local notification
        appDelegate.automaticBackupNotification = [[UILocalNotification alloc] init];
        if(appDelegate.automaticBackupNotification){
            //Repeat the notification according to frequency
            if ([backupFrequencyLabel.text isEqualToString:@"Daily"]) {
                appDelegate.automaticBackupNotification.repeatInterval = NSDayCalendarUnit;
            }
            if ([backupFrequencyLabel.text isEqualToString:@"Weekly"]) {
                appDelegate.automaticBackupNotification.repeatInterval = NSWeekCalendarUnit;
            }
            else {
                appDelegate.automaticBackupNotification.repeatInterval = NSMonthCalendarUnit;
            }

            //Set fire date to alert time
            NSCalendar *calendar = appDelegate.automaticBackupNotification.repeatCalendar;
            if (!calendar) {
                calendar = [NSCalendar currentCalendar];
            }

            NSDateComponents *components = [[NSDateComponents alloc] init];
            components.day = 1;
            //NSDate *nextFireDate = [calendar dateByAddingComponents:components toDate:[NSDate date] options:0];
            //appDelegate.automaticBackupNotification.fireDate = nextFireDate;
            appDelegate.automaticBackupNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:20.0];

            //Set time zone to default
            appDelegate.automaticBackupNotification.timeZone = [NSTimeZone defaultTimeZone];

            // schedule notification
            UIApplication *app = [UIApplication sharedApplication];
            [app scheduleLocalNotification:appDelegate.automaticBackupNotification];

            NSLog(@"Backup Fire Date: %@", appDelegate.automaticBackupNotification.fireDate);
        }
    }
    else {
        [defaults setValue:@"0" forKey:@"automatic"];
        if(appDelegate.automaticBackupNotification){
            [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.automaticBackupNotification];
        }
    }

    [defaults synchronize];
}

但是,当应用程序委托(delegate)收到通知时,它会触发条件的“其他”部分。有什么办法可以区分不同的通知吗?还是我做错了什么?

干杯,

酪蛋白

最佳答案

NSNotification 对象有一个名为 userInfo 的属性。它是一个 NSDictionary,您可以在创建通知的地方设置一些值,并在您收到通知的地方检查它们。

关于iphone - 如何判断收到了哪个本地通知? Objective-C ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11793990/

相关文章:

iphone - 如何在不更改 iPhone 应用程序中的设备方向的情况下更改应用程序的方向

ios - 如何将高度约束添加到层次结构中的顶部 UIView?

ios - 在 IBOutletCollection 中保留图像

iphone - 我的应用程序在播放声音时崩溃

iphone - 如何从 MKMapView 中删除所有注释而不删除蓝点?

iphone - 结果未返回 FMDB 中的任何数据

iphone - segue通过后图片将不会显示

iphone - 从联系人处获取地址和电话号码?

ios - ios拼图游戏几轮后崩溃

ios - 触摸时未检测到自定义 SKSpriteNode