Skip or Disable iOS Notification When App Is In Background
Asked Answered
P

1

6

I am currently saving a flag to disable notifiations in my user preferences. I use that flag in my didReceieveRemoteNotifications to show or skip the notification that comes in. However, when my app is in the background it still shows and I believe I have all my methods setup correctly and it's not being hit. Is there a way to catch this notification while app is in the background and skip it from showing?

  NSNumber* enabled = [Helper getBooleanPreference:[Config subscriptionsEnabled]];

        if(enabled == nil || [enabled integerValue] == 1) {
            completionHandler(UIBackgroundFetchResultNewData);
        }
Paterfamilias answered 17/1, 2017 at 18:51 Comment(0)
B
5

From Apple documentation

The sending of a silent notification requires a special configuration of the notification’s payload. If your payload is not configured properly, the notification might be displayed to the user instead of being delivered to your app in the background. In your payload, make sure the following conditions are true:

The payload’s aps dictionary must include the content-available key with a value of 1.

The payload’s aps dictionary must not contain the alert, sound, or badge keys.

So your push should contain content-available with value 1 and not contain alert, sound, or badge keys.

In that case you have to show UILocalNotification if you want to notify the user.

Bluebeard answered 17/1, 2017 at 19:6 Comment(4)
How would that even work as I want to use the alert data in the first place?Paterfamilias
I described you the only way you could receive Push notification inside the app and decide if you have to show it to the user or no. If you want to receive some data in the push, you can get it through custom payload developer.apple.com/library/content/documentation/….Bluebeard
IMPORTANT Background update notifications are not meant as a way to keep your app awake in the background beyond quick refresh operations, nor are they meant for high priority updates. APNs treats background update notifications as low priority and may throttle their delivery altogether if the total number becomes excessive. The actual limits are dynamic and can change based on conditions, but try not to send more than a few notifications per hour.Lasser
@Bluebeard I'm still getting sound with the silent notification eventhough there's no sound in the payload.Arcboutant

© 2022 - 2024 — McMap. All rights reserved.