'Add vibration the background for local notifications
I'm adding a video chat to my app and use PushKit to get a pushNotification when the app is in the background. I Can't use the CallKit for video as it mess up the SDK I'm using so I have added a local push notification that fire up from the PushKit delegate method.
I'm wondering how Whatsapp does it with their Video call. For now they are showing a push notification but in a way that I can't recognize. The push is fired up with two vibrations and after two seconds there is a new push that overlaps the first notification and you can feel another two vibrations and so on until you answer. How do they do it as you can't add vibration over and over again and how do they delete the notification and establish a new one in the background as the NSTimer is not working in the background. If I add [[UIApplication sharedApplication] beginBackgroundTaskWithName:expirationHandler:]
then I can use the timer for 180 seconds but only if the app was active.
So the real problem is how to add a local notification that can repeat itself few times and also add vibration to it?
Solution 1:[1]
For iOS 10 and later. Use UNUserNotificationCenter:
let notificationContent = UNMutableNotificationContent()
// this sets sound + vibration
notificationContent.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 1,
repeats: false
)
let request = UNNotificationRequest(
identifier: "notification_identifier",
content: notificationContent,
trigger: trigger
)
UNUserNotificationCenter.current().add(request) { _ in }
Solution 2:[2]
You can create a notification in the future and when it gets called cancel all the previous ones and reschedule until you're satisfied.
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = body;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.fireDate = [NSDate new]; //<-Set the date here in 10seconds for example
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Then in application didReceiveLocalNotification:(UILocalNotification *)notification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
application cancelAllLocalNotifications;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | nayooti |
Solution 2 | ahbou |