2

I'm trying to replicate how WhatsApp signals the callee's device about an incoming call when the caller starts a call to a user who has this WhatsApp closed. According to the lock screen, the call receiver's device seems to be repeatedly receiving push notification with an interval of approximately 1 second, saying "Call from UserName". But most notably, the notifications do not pile up. It appears that every notification about an incoming call is replaced by the next such notification. And when the caller drops the call, the last incoming-call notification on the callee end is replaced by "Missed call" notification.

How can I achieve push notification replacement/deletion in this way?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
  • Possible duplicate of [How does Facebook Messenger clear push notifications from the lock screen if you’ve read them on desktop?](http://stackoverflow.com/questions/21986886/how-does-facebook-messenger-clear-push-notifications-from-the-lock-screen-if-you) – xoudini Jul 19 '16 at 10:03

3 Answers3

2

You can use the "apns-collapse-id ". it will replace the Notification contents with the same id

https://developer.apple.com/documentation/usernotifications/unnotificationrequest/1649634-identifier

https://medium.com/the-guardian-mobile-innovation-lab/how-to-replace-the-content-of-an-ios-notification-2d8d93766446

i'm SRK
  • 41
  • 4
  • "apns-collapse-id " will NEVER replace the Notification. It will make it collapse if there exist an other notification with same "apns-collapse-id " identifier – Zeeshan Badshah Dec 08 '22 at 13:56
1

WhatsApp uses silent notifications to trigger the display of local notifications. Local notifications can be replaced by the app. This was the last time I reversed engineered their process. They probably use Push Kit messages now, as they are a VoIP app.

Avi
  • 7,469
  • 2
  • 21
  • 22
  • Thanks. And how can I run any code to schedule local notifications if the app was force closed? – Desmond Hume Jul 19 '16 at 10:15
  • If you use APNS, there are no guarantees. iOS will probably not run the app to process them. WhatsApp has the same issue (if they haven't moved to PK). If you use PK, iOS will launch your app in the background to process them, because PK messages are always silent. – Avi Jul 19 '16 at 10:17
-1

Whatsapp, skype or any other VOIP related app uses push kit.

Use content-available = 1 in payload to make it silent push notification.

Silent push notification will invoke your app in background even if your app is in kill state ( Terminated state ), so it will allow you to schedule local notification.

  • Once you have payload for incoming call schedule local notification

  • Once you get payload for missed call, cancel incoming call local notification and schedule missed call local notification

  • Note - Incoming or missed call local notification object, always keeps in NSUserDefault to ensure its availability to cancel even if you restart your device.

  • Payload related details you can keep in localnotification.userInfo

Pushkit code

import UIKit
import PushKit


class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate{



func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


    let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
    application.registerForRemoteNotificationTypes(types)

    self. PushKitRegistration()

    return true
}



//MARK: - PushKitRegistration

func PushKitRegistration()
{

    let mainQueue = dispatch_get_main_queue()
    // Create a push registry object
    if #available(iOS 8.0, *) {

        let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)

        // Set the registry's delegate to self

        voipRegistry.delegate = self

        // Set the push type to VoIP

        voipRegistry.desiredPushTypes = [PKPushTypeVoIP]

    } else {
        // Fallback on earlier versions
    }


}


@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
    // Register VoIP push token (a property of PKPushCredentials) with server

    let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
        count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")

    print(hexString)


}


@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
    // Process the received push

    // As per payload schedule local notification / cancel local notification


}

}

Kind of pushkit payload

{
    "aps": {
        "content-available": 1,
        "screen": "IncomingCall",
        "alertTitle": "Mr ...",
        "alertBody": "Call from ...",
        "category": "INCOMINGCALL_CATEGORY",
        "data": "Any specific data you want to pass"
    }
}
Hasya
  • 9,792
  • 4
  • 31
  • 46
  • This does not ensure you will get the call notification. Using PushKit with VoIP used to be the option, but now it forces you to use CallKit. Whatsapp has a special entitlement btw. – tomasyany Jan 05 '21 at 14:54