Caner Balım
Caner Balım

Reputation: 556

Swift Cancel Push Notification Before Showing

I developed an app with Swift for IOS 10.2 . I use Apple Push Notification from server. In this app I use UserDefaults for user settings.
I want that If the user wants to receive news of the day, My Application Allow showing push notification for news category. As a result Can I manage push notification before showing to user ?
Do I have to keep the settings on the server ? Any suggestion ? Thanks.

Upvotes: 0

Views: 2416

Answers (2)

Rafael Setragni
Rafael Setragni

Reputation: 170

Apple notification team sometimes (almost every time) takes some pretty stupid decisions, as:

  • Hightely attatched categories identifiers with action buttons and Notification Content Extension, resulting in a vast and unnecessary cartesian product of registered category identifiers on the system.

  • initial fire date remotion from the new UNNotificationTrigger in iOS 10 or higher.

  • Creating UNPushNotificationTrigger and losing all the others
    UNNotificationTrigger capabilities for push
    notifications.

...and not be able to cancel a push notification using Notification Service Extension is a pretty good example of it. Why do not cancel a invalid or corrupt push notification before it shows to the user using Notification Service Extension?

There is no other valid explanation for this but development team dementia or ill will.

BUT you can "troll the system"! YES!!!

Build a invalid and empty notification content and send it back to Notification Service to digest and dies choked up. Inside your UNNotificationServiceExtension:

public override func didReceive(
    _ request: UNNotificationRequest,
    withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
){
    self.contentHandler = contentHandler
    self.content = (request.content.mutableCopy() as? UNMutableNotificationContent)
    
    if let content = content {
        
        print("Valid FCM received")
        contentHandler(content)
    }
    else {
    
        // Invalidates the notification
        print("Invalid FCM received")
        contentHandler(UNNotificationContent())
    }
}

Take that, big brother.

Upvotes: 0

Puneet Sharma
Puneet Sharma

Reputation: 9484

Apple introduced Notification Service Extension in iOS 10. You can use this extension to modify the content of the notification sent by APNS. This extension allows you to download image/video/gif content before showing to the user or modify the content of the notification like title, subtitle, description. But, you won't be allowed to cancel the notification altogether.

Although if you save the Notification settings in Suite UserDefaults(by making use of AppGroups), you have access to those settings in your Notification Service extension and after reading from the settings you can change the content of the notification to a general notification and use API to send the notification settings to your external server and stop sending that particular notification to the client. But yes, the logic of cancelling/stopping particular notification has to be written on the server.

Hope this helps you.

Upvotes: 1

Related Questions