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

╄→гoц情女王★ 提交于 2019-11-26 21:56:28

问题


I'm working on audio/video call and trying to get incoming call notification loop for 1 minute like WhatsApp shows in iOS when app is background, Notification banner hide and show with ringtone for 1 minute.

I have tried this code, it triggers only single time:

 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");
        }
    }];

How can I achieve this? I'm using UserNotifications.framework (iOS 10) and PushKit.


回答1:


What I would suggest is that the local notification is only visible for that particular time. What you have to do is set a local notification or notification at the time of call. When the notification fires, in the delegate method of the notification you will have to build a custom logic with help of NSTimer.

Create a view similar to push notification/ or view which you want to show for the call. Add it to the Application Window so it is shown on top of all views. After 3 sec remove this view and in the logic you can show the same view for 1 min.




回答2:


iOS 10 CallKit is much better, no more notification but system call UI, just like the native call. There are some sample codes at Github, which are easier to read than apple's Speakerbox sample.



来源:https://stackoverflow.com/questions/41845576/ios-10-how-to-show-incoming-voip-call-notification-when-app-is-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!