0

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

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

另外,我尝试在 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);
        }
    }];
4

2 回答 2

0

就我而言,添加一些文本来content.body解决问题:

content.body = 'Any text'
于 2018-09-20T08:33:18.823 回答
0

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

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

所以尝试使用UNTimeIntervalNotificationTrigger而不是UNCalendarNotificationTrigger

NSTimeInterval timeInterval = [fireDate timeIntervalSinceDate:[NSDate date]];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
于 2018-02-21T08:58:55.240 回答