4

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Abhishek Thapliyal
  • 3,497
  • 6
  • 30
  • 69
  • @Jayprakash Dubey : Can you help in this question ? – Abhishek Thapliyal Jan 31 '17 at 07:59
  • You want a notification here for continuous 1 min or at 1 min interval? – gurmandeep Jan 31 '17 at 11:09
  • i want notification to show for 3secs and hide and this process should be run only for 1 minute just like whatsapp incoming call when app is in background in ios – Abhishek Thapliyal Jan 31 '17 at 11:13
  • @AbhishekThapliyal Im facing same problem, can u help me how you achieive this. – siva krishna May 02 '17 at 08:55
  • @siva krishna : Use background task with expiration handler https://www.raywenderlich.com/29948/backgrounding-for-ios Show Local notification with timer for 1 minute play audio in same thread Please upvote my question as well :) – Abhishek Thapliyal May 02 '17 at 09:03
  • @AbhishekThapliyal I didn't found any code to play audio. I was set up a notification. But some times default sound is coming, some times that also not coming, I'm using twillio(https://www.twilio.com/docs/guides/video/conversations#setting-up-a-conversation_1) for voip calls. – siva krishna May 02 '17 at 10:57
  • http://stackoverflow.com/questions/10329291/play-a-short-sound-in-ios – Abhishek Thapliyal May 02 '17 at 11:12
  • @AbhishekThapliyal 1. Is it possible to get current ringtone of iphone and play that sound. 2. What my cocern is if i'm getting video call there is no need to accept from my side, It should automatically get accepeted. When the app is foreground. When the app is in background alos its getting accepted but the who called is seeing a balank screen since the app is background. so if i play sound (like caller ringtone). then the chances of coming to foreground is high. So when i want to play sound since my video call is already been initiated, my sound is not playing – siva krishna May 04 '17 at 10:22
  • 1 : https://github.com/TUNER88/iOSSystemSoundsLibrary 2 : That can't be possible any human action is required for that and secondly apple doesn't allow to show any view when app is in background so it is only possible way to show/animate local notification – Abhishek Thapliyal May 04 '17 at 13:30
  • @AbhishekThapliyal did u find any answer please help me. – Siva Sankar Oct 31 '19 at 06:55

2 Answers2

2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
gurmandeep
  • 1,227
  • 1
  • 14
  • 30
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.

Qiulang
  • 10,295
  • 11
  • 80
  • 129
  • please help me on this issue : http://stackoverflow.com/questions/42266258/ios-9-beginbackgroundtaskwithexpirationhandler-is-getting-called-before-timout/42274565?noredirect=1#comment71732371_42274565 – Abhishek Thapliyal Feb 20 '17 at 07:27
  • I meant my answer to another question. Not this one :) – Qiulang Feb 20 '17 at 08:14