【问题标题】:UILocalNotification every 30 secondsUILocalNotification 每 30 秒
【发布时间】:2013-10-13 19:40:56
【问题描述】:

我正在尝试使用以下逻辑将UILocalNotification 设置为每 30 秒运行一次,但它似乎行为不端。有两个问题:

  1. 当通知被触发时,似乎同时有很多通知,而不是每 30 秒 1 个。
  2. 应用程序图标徽章数量似乎没有增加。它只是保持在 1。

请有人帮我找出我做错了什么吗?

// Create 'base' notification we can use
UILocalNotification *baseNotification = [[UILocalNotification alloc] init];
baseNotification.timeZone = [NSTimeZone defaultTimeZone];
baseNotification.repeatInterval = NSMinuteCalendarUnit;
baseNotification.alertBody = @"My Message.";
baseNotification.alertAction = @"My Alert Action";
baseNotification.soundName = UILocalNotificationDefaultSoundName;

UILocalNotification *alertOne = [baseNotification copy];
alertOne.applicationIconBadgeNumber++;
alertOne.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
[[UIApplication sharedApplication] scheduleLocalNotification:alertOne];

UILocalNotification *alertTwo = [baseNotification copy];
alertTwo.applicationIconBadgeNumber++;
alertTwo.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
[[UIApplication sharedApplication] scheduleLocalNotification:alertTwo];

【问题讨论】:

  • “当通知被触发时”是什么意思?您是否一次看到许多通知警报,或者方法application:didReceiveLocalNotification: 被多次调用?
  • 我的意思是我同时看到许多通知警报。
  • 您是否通过致电cancelAllLocalNotifications 取消所有之前的预定提醒?

标签: iphone ios objective-c ios7 uilocalnotification


【解决方案1】:

试试这个。

UILocalNotification *baseNotification = [[UILocalNotification alloc] init];
baseNotification.timeZone = [NSTimeZone defaultTimeZone];
baseNotification.repeatInterval = NSMinuteCalendarUnit;
baseNotification.alertBody = @"My Message.";
baseNotification.alertAction = @"My Alert Action";
baseNotification.soundName = UILocalNotificationDefaultSoundName;

UILocalNotification *alertOne = [baseNotification copy];
alertOne.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
alertOne.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;

UILocalNotification *alertTwo = [baseNotification copy];
alertTwo.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
alertTwo.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;

【讨论】:

    【解决方案2】:

    目前无法实现带间隔的自定义重复。

    但是,通知系统最多可以排队 64 条通知,因此您可以做的最接近的事情是手动设置所需数量的通知(每个通知都有不同的徽章编号和不同的 fireDate),然后当您的通知列表不足时,通过设置新通知列表来更新它们。

    这将返回您在队列中的通知数量:

    [[[UIApplication sharedApplication] scheduledLocalNotifications] count]
    

    我还建议您阅读这篇文章以获得更多帮助:

    iOS badge number live update

    祝你好运!

    【讨论】:

      【解决方案3】:

      关于第二点,您增加的是副本的徽章编号,而不是原始通知。由于原件的徽章编号为零,因此您总是会得到一个徽章编号为零的副本,并且增加它会使其始终为 1。

      解决方法是在复制之前增加原始通知的徽章:

      ...
      baseNotification.applicationIconBadgeNumber++;
      UILocalNotification *alertOne = [baseNotification copy];
      alertOne.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
      [[UIApplication sharedApplication] scheduleLocalNotification:alertOne];
      
      baseNotification.applicationIconBadgeNumber++;
      UILocalNotification *alertTwo = [baseNotification copy];
      alertTwo.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
      [[UIApplication sharedApplication] scheduleLocalNotification:alertTwo];
      

      【讨论】:

        【解决方案4】:

        根据 NSObject 类参考:

        copy - 返回copyWithZone返回的对象:

        而copyWithZone 返回一个浅拷贝。 所以它就像所有通知都具有相同的属性。 因此,徽章编号始终为“1”,并且所有通知的 fireDate 都相同。即您申请的最后一个。 因此,通知会同时被触发。

        希望,它会有所帮助。

        【讨论】:

        • 谢谢 - 你知道是否有办法可以复制它们以使值不完全相同?
        • 为什么不创建一个新实例?据我所知,要进行深度复制,您必须实现该对象的 copyWithZone : ,这在 UILocalNotification 类的情况下不是解决方案。
        【解决方案5】:

        我认为您每 30 秒收到很多通知是因为您没有取消之前的通知。在代码顶部添加这一行。

        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-13
          • 2012-03-26
          相关资源
          最近更新 更多