【问题标题】:How to tell which local notification has been received? Objective - C如何判断收到了哪个本地通知?目标 - C
【发布时间】:2012-08-03 10:33:46
【问题描述】:

我有这段代码根据收到的本地通知调用不同的 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];
}

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

干杯,

泰辛

【问题讨论】:

    标签: iphone objective-c ios uilocalnotification


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      只需尝试设置UILocalNotificationuserInfo 属性,就像这样,

      NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"YOUROBJECT" forKey:@"TESTKEY"];
      YOURNOTIFICATION.userInfo = userDict;
      

      当 UILocalNotification 触发时,此方法将被调用,

      - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
      {
           NSDictionary *dict = [notification userInfo];
           id obj = [dict objectForKey:@"TESTKEY"];
      }
      

      根据您在设置UILocalNotification时设置的userInfo,您可以找出调用了哪个notification

      【讨论】:

        【解决方案3】:

        我个人使用通知名称来知道哪个通知推送或已收到

        //add local observer
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mathodCalled:) name:[NSSTring StringWithFormat :@"plop"] object:nil];
        
        ...
        
        //push an event manually 
        [[NSNotificationCenter defaultCenter] postNotificationName:eventName object:[NSSTring StringWithFormat :@"plop"]];
        
        ...
        
        - (void)methodCalled :(NSNotification*)aNotification{
            if([aNotification.name isEqualToString:@"plop"]){
                // do something
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2018-04-19
          • 1970-01-01
          • 2013-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-09
          相关资源
          最近更新 更多