1

On Android, I can send a silent push that gets delivered to the system tray. On iOS, I'm not sure if this is possible. I removed the alert property from aps, and I'm sending an empty string for sound. What I'm seeing is that the badge count is updated silently (which is good), but there is nothing in Notification Center. So is there a way to send a push to NC without a sound?

Note that I do not want the push to pop up on the screen (i.e. the user should not see it unless they swipe down on the screen to reveal what's been delivered. In other words, I'm trying to match the behavior on Android).

user246392
  • 2,661
  • 11
  • 54
  • 96
  • This should work {"aps":{"alert":"Hello","badge":1,"category":"your_category_key"}}. Basically you should not have sound key in payload. – lloydTheCoder Mar 03 '20 at 07:23
  • 1
    If I remove the sound key completely, there is no sound. That's good, but the problem is the notification still pops up on the screen. I want the notification to go to NC directly. How do I achieve this? – user246392 Mar 04 '20 at 18:05
  • You can use this code while registering for notification in AppDelegate, `let notificationCenter = UNUserNotificationCenter.current() notificationCenter.requestAuthorization( options:[.badge, .sound, .alert, .provisional]) { }` – lloydTheCoder Mar 05 '20 at 12:39
  • .provisional will make sure that notifications are quietly delivered user and will be placed in notifcation centre. This feature is called provisional authorisation and is introduced in iOS 12. – lloydTheCoder Mar 05 '20 at 12:53
  • @lloydTheCoder Can you confirm that it is not possible to make some notifications provisional while making others non-provisional? On Android, I could use different channels. I'm surprised iOS does not provide this functionality. – user246392 Mar 05 '20 at 16:51
  • Refer this link https://stackoverflow.com/questions/28167185/remove-sound-from-notification/38377182 – Oshitha Wimalasuriya Mar 06 '20 at 08:52
  • @ user246392 selectively it is not possible as far as I know. – lloydTheCoder May 07 '20 at 06:02

5 Answers5

0

yes, apple support it, the push data format is

{
    "aps":{
        "aa":"123",
        "content-available":1
    }
}

important key is this
"content-available":1

  • I set `content-available` to 1. I also set `mutable-content` to 1. It doesn't work for me. I can intercept the push in `didReceiveRemoteNotification` but it doesn't show in NC. – user246392 Mar 03 '20 at 05:11
0

If you don't want sound then don't send sound param in payload.

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 1
    },
    "acme1" : "test"
}

also you can remove .alert and .sound from willPresent.. method of push in appdelegate.swift file

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    completionHandler([.badge]) //remove .sound, .alert
}
Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
  • I haven't tried this yet, but I thought `alert` will show a popup effect. I don't want that either. If I remove it and the sound, then I don't see the notification in Notification Center. Only the badge gets updated. – user246392 Mar 03 '20 at 05:49
  • then you can remove `.alert` and `.sound` from `willPresentNotification` method of push. – Nirav Kotecha Mar 03 '20 at 05:53
  • It seems `willPresentNotification` is only called when the app is in the foreground. Is this true? I have implemented a custom `UNUserNotificationCenterDelegate`. The method is only called when a push is delivered in the foreground. – user246392 Mar 03 '20 at 06:08
  • Read the discussion section here: https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter?language=objc Looks like this is only for the foreground. – user246392 Mar 03 '20 at 06:12
0

The only way to achieve what you want to do will be to specify "provisional" when asking the notification permission on the device, which is supported on iOS >= 12. This makes the notifications received be completely silent and appear only on the notification center without interrupting the user. However, the user can customize this and choose to be "interrupted" by the notifications, and this permission will also prevent all other notifications from "interrupting" the user.

If you want to selectively specify some notifications to only go to the Notification Center, while others make sounds and show as notification alerts to the user, then the answer to your question is no, its not possible.

elyalvarado
  • 1,226
  • 8
  • 13
0

I am not sure if I understand your question right. But if you just want to send a silent notification to iOS, wake up the app in background and let it handle the notification, this can be done, see the docs.
Essentially, you have

  • to enable the remote notifications background mode in the Signing & Capabilities tab of your target, and
  • to create a remote notification with an aps dictionary that includes only the content-available key:

    { "aps" : { "content-available" : 1 }, "key1" : "xxx", "key2" : 123 }

To receive the silent notification, the system will then wake your app in the background. It then calls your app delegate’s application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
0

Yes just send it without sound key.If you add blank sound key then also default sound will come for sure, so not to add key for no sound.