Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide notifications received through NotificationService Extension

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.

like image 381
Leguman Avatar asked Mar 16 '18 20:03

Leguman


People also ask

What is remote notification extension on Iphone?

A UNNotificationServiceExtension object provides the entry point for a notification service app extension. This object lets you customize the content of a remote notification before the system delivers it to the user.

What is notification service extension?

The Notification Service Extension was designed to intercept any incoming push notification our applications receive, allowing us to modify its payload content — for example, changing the title, decrypting any encrypted data or even downloading media attachments.

What is MoEngage push notification?

MoEngage Push Amplification helps marketers solve the push delivery issue and reach more potential customer devices that are cut off from FCM. With push amplification, brands can improve their push delivery rates by 40%, thereby reaching more app customers and driving more conversions.


1 Answers

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.".
like image 137
fredpi Avatar answered Oct 13 '22 00:10

fredpi