iOS 10:如何在应用程序处于后台时显示传入的VOIP呼叫通知?

我正在进行音频/视频通话,并尝试将来电通知循环播放1分钟,例如当应用程序为背景时,WhatsApp在iOS中显示,通知横幅隐藏并显示铃声1分钟.

我试过这段代码,它只触发一次:

 UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString stringWithFormat:@"Video Call from %@",userId];
    content.body = @"";
    content.userInfo = [userInfo mutableCopy];
    content.sound = [UNNotificationSound soundNamed:@""];

    NSDate *now = [NSDate date];
    now = [now dateByAddingTimeInterval:3];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [calendar setTimeZone:[NSTimeZone localTimeZone]];

    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:now];

    UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
    UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"INCOMING_VOIP_APN" content:content trigger:trigger];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"PUSHKIT : INCOMING_VOIP_APN");
        }
    }];

我怎样才能做到这一点?我正在使用UserNotifications.framework(iOS 10)和PushKit.

最佳答案
我建议的是,本地通知仅在特定时间可见.您需要做的是在通话时设置本地通知或通知.触发通知时,在通知的委托方法中,您必须在NSTimer的帮助下构建自定义逻辑.

创建类似于要为呼叫显示的推送通知/或视图的视图.将其添加到“应用程序窗口”,使其显示在所有视图的顶部. 3秒后删除此视图,在逻辑中您可以显示相同的视图1分钟.

转载注明原文:iOS 10:如何在应用程序处于后台时显示传入的VOIP呼叫通知? - 代码日志