【问题标题】:Local Notification Displays Twice本地通知显示两次
【发布时间】:2013-08-23 06:43:34
【问题描述】:

有时我的应用的本地通知会被触发(显示)两次。我没有使用模拟器,而是使用真实设备。我一直试图获得一个重现步骤,但我无法做到。每次我使用断点/nslog 完成该过程时,我总是会收到 1 个安排好的通知。有了这个,我假设我只会显示/触发 1 个通知。但是,有时我会收到两个通知。 我已经在互联网上搜索了答案,但我无法获得太多信息。这里有人经历过同样的事情吗?你是如何解决这个问题的?

- (void)scheduleAllNotifications
{
    if (_isEnabled && [[NSUserDefaults standardUserDefaults] boolForKey:NotificationsUserDefaultsKey]) {
        [self updateFireDates];
        for (NSDate *fireDate in fireDates) {
            UILocalNotification *notification = [[UILocalNotification alloc] init];
            notification.fireDate = fireDate;
            notification.alertBody = @"Message";
            notification.alertAction = @"View";
            notification.soundName = @"notificationsound.mp3";
            [notifications addObject:notification];

            [[UIApplication sharedApplication]scheduleLocalNotification:notification];
        }
    }
}



- (void)cancelAllNotifications
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [notifications removeAllObjects];

}

- (void)updateFireDates
{
    [fireDates removeAllObjects];
    NSDate *now = [NSDate date];
    NSDate *fireDate = [NSDate dateWithTimeInterval:THREEDAYS sinceDate:now];
    if (fireDate){[fireDates addObject:fireDate];}
}

每次应用激活时都会调用cancelAllNotifications 每次应用退出活动时都会调用 shceduleAllNotifications

【问题讨论】:

  • 显示您的代码如何添加和检查通知。
  • 我已经添加了我的代码。谢谢

标签: ios notifications uilocalnotification localnotification


【解决方案1】:

Bart Doe 是对的,这些是之前测试运行的杂散通知。

要解决此问题,请在 - (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 内的 AppDelegate 中调用 [[UIApplication sharedApplication] cancelAllLocalNotifications];,以清除所有之前的预定通知。

【讨论】:

  • 这里有同样的问题。杂散通知。我所做的只是卸载该应用程序,一旦我再次安装它们就消失了。希望在生产时不会发生这种情况。
【解决方案2】:

由于您似乎处于项目的构建/调试阶段,如果这些双重通知实际上是旧通知,我不会感到惊讶。我碰巧认为该设备创建了奇怪的杂散通知,而实际上在开发/测试期间我自己创建了错误的通知。

你可以做的是在 userInfo 字段中添加一些额外的调试信息。您可以将内部版本号或创建日期/时间之类的东西放在那里。然后在通知触发时记录这些内容。

不过,您可能确实遇到了错误: local notification "didReceiveLocalNotification" calls twice

【讨论】:

  • 其实,发生的时候我还没有处于调试状态。我只是在它发布后玩它。
猜你喜欢
  • 2021-10-16
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-28
相关资源
最近更新 更多