【问题标题】:All delivered Scheduled Notifications are cleared from Notification Center when one is tapped当一个被点击时,所有发送的计划通知都会从通知中心清除
【发布时间】:2018-01-30 09:16:50
【问题描述】:

我为用户安排了多个本地通知。所有这些也都在指定的时间交付。但是,当我尝试从通知中心打开其中任何一个时,它们都被清除了。

在合理的情况下,我不希望所有这些都从通知中心清除,只希望那些被点击打开的。

另外,我尝试在 AppDelegate.m 的代码下方评论,但问题仍然存在。 [[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];

谁能告诉我这可能是什么问题,即使我点击打开其中一个,我的计划通知也会从通知中心清除?

下面是我用来安排本地通知的代码 -

NSDateComponents *components = [SSUtility hoursMinuteAndSectionsForDate:date];

    NSInteger hour = [components hour];
    NSInteger minute = [components minute];

    NSLog(@"Hour %ld Min %ld ", hour,minute);

    UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

    /* Set notification */

    UNMutableNotificationContent *content = [UNMutableNotificationContent new];
    content.body = body;
    // content.categoryIdentifier=NSNotificationC;
    content.sound = [UNNotificationSound defaultSound];
    content.userInfo = userInfo;
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                          content:content
                                                                          trigger:trigger];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (error != nil) {
            SSLOG(@"Something went wrong: %@",error);
        }
    }];

【问题讨论】:

  • 您找到解决方案了吗?我有同样的问题
  • 事实证明,这是您使用 UNUserNotificationCenter 而不是弃用的 UILocalnotification 时的默认行为。

标签: ios objective-c uilocalnotification unusernotificationcenter unnotificationrequest


【解决方案1】:

所以我不相信这是默认行为所以我发现:

  • 如果您使用UNCalendarNotificationTrigger,那么所有发送的提醒都会在点击时删除
  • 如果您使用UNTimeIntervalNotificationTrigger,则已发送的提醒仍保留在通知中心

所以尝试使用UNTimeIntervalNotificationTrigger 而不是UNCalendarNotificationTrigger

NSTimeInterval timeInterval = [fireDate timeIntervalSinceDate:[NSDate date]];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];

【讨论】:

    【解决方案2】:

    在我的例子中,添加一些文本到content.body 解决问题:

    content.body = 'Any text'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多