Hide notifications received through NotificationService Extension
Asked Answered
L

1

15

I'm using the NotificationService Extension target to be able to modify push notifications before they are presented. It's working well but there is some notifications that I would like to not present to the user after they have been processed.

I'v tried to set the bestAttemptContent title and body to "" but it return the full notification.

I've also tried setting the aps["content-available"] to 1 and the body to "" but I get the same effect.

Laurustinus answered 16/3, 2018 at 20:48 Comment(0)
T
21

The Situation

I actually tried to achieve the same thing as you, managing what kind of notifications get delivered to a specific device using the notification service extension locally. However, I had to figure out it's by design that...

  • ... you cannot cancel the notification via a discrete api.
  • ...if you empty the content of the notification, the notification will be delivered as if the notification service extension never has been called.

This is because Apple doesn't want you to spam a user's phone with notifications (even or especially when processed in the background and not even delivered), given this could drain battery life e. g.

The Solution

You have four possible choices on what to do now:

  • Use silent push. Then trigger local notifications. Note: Silent push isn't always reliable.
  • Use VoIP push. This is what WhatsApp uses e. g. and it would allow for such thing as notification cancelling as far as I know. However, Apple is strict when it comes to app review: Only VoIP apps are allowed to use VoIP push (I take the guess that's why WhatsApp introduced voice calls, because they needed VoIP push for decrypting their messages in the background when the notification service extension wasn't an option yet under iOS 9).
  • Handle any custom cancelling logic server-side by only sending notifications to users that ought to receive those notifications right now.
  • Manipulate the notification text to something not empty that nonetheless hides the original notification text. This may be useful, if you e. g. send out a push that a pro version of your app is available at a discount currently: for existing pro users, you could just edit the text to read "Thanks for being a pro.".
Triphthong answered 16/3, 2018 at 23:13 Comment(3)
As of iOS 13 VoIP pushes are not a solution anymore, as you must report a new incoming call through CallKit when receiving a VoIP push.Safe
Adding a little bit to this great answer: 1. Since iOS 13.3 there's new entitlement com.apple.developer.usernotifications.filtering that lets NotificationExtension to filter notifications. You can request one for yourself. 2. There's another special entitlement com.apple.developer.pushkit.unrestricted-voip which let's app keep pre-iOS13 VoIP pushes behavior. As of today, many big app store app are using it: Signal, Telegram, FB Messenger, Skype, Whatsapp. There's no official way to get one.Boesch
This is because Apple doesn't want you to spam a user's phone with notifications. I dont understand this. How can we spam from extension. All we are given is some function, which we should call after processing the notification. Why can't we just call it with say "nil" to stop the system from showing the notification? Will checkout com.apple.developer.usernotifications.filteringZak

© 2022 - 2024 — McMap. All rights reserved.