iOS 10:如何在后台显示VOIP来电通知?

iOS 10: How to show incoming VOIP call notification when app is in background?

我正在处理 audio/video call 并尝试接听来电 notification 循环 1 分钟,就像 WhatsApp 在 iOS 中显示的那样,当应用程序处于后台时,Notification banner 隐藏并以铃声显示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.

的帮助下构建自定义逻辑

创建一个类似于推送通知的视图/或您要在通话中显示的视图。将它添加到应用程序 Window 以便它显示在所有视图的顶部。 3 秒后删除此视图,在逻辑中您可以显示相同的视图 1 分钟。

iOS 10 CallKit 好多了,不再通知而是系统调用UI,就像原生调用一样。 Github有一些示例代码,比apple的Speakerbox sample更易读。