3

I would like to use the UserNotificationFramework on iOS 10 to send myself data only notifications -- i.e., with no user facing text or sound. Basically to use it as a persistent timer to be able to trigger the app, if running when it fires, to check for things like session timeout due to token expiry.

Kind of like an analog to a "content-available" faceless push notification in a local notification.

I have it all working except I cannot get a notification to fire without some sort of user facing data needing to be involved.

Here are the relevant pieces of the UNMutableNotificationContent of the UNNotificationRequest that is added to the UNUserNotificationCenter to trigger the notification.

//  content.body = "Expiration"
//  content.sound = UNNotificationSound(named: "silent-short.wav")
    content.threadIdentifier = typeIdentifier
    content.userInfo = ["1":"One"]
    content.setValue("YES", forKey: "shouldAlwaysAlertWhileAppIsForeground")

As above the notification does not fire at all. If I uncomment either one of those lines -- does not matter which one -- it will fire. But I don't want a text notification banner displaying to the user (in or outside the app) so the first one does not help. I tried to use a silent sound, so that the system can think it is playing the sound but the user experiences nothing. But the system throws in a handy vibrate. Which in this case is not so handy. Is there a way to get the notification to not vibrate? Or any other way to get a local data only notification?

chadbag
  • 1,837
  • 2
  • 20
  • 34
  • Inherit in its name is **user**notification ie something which _user_ sees. AFAIK there is no way to do that using `UNNotificationRequest` , you must achieve this using **silent**-notification which inherit in its name is silent ie something which the _user_ doesn't see. – mfaani Sep 01 '17 at 14:42

1 Answers1

3

You cannot schedule a local notification to act like a silent push notification and not show up in the notification centre. The UserNotifications framework was designed to display data to the user in the form of a notification.

You can only achieve silent notifications using push notifications, there is no way at the moment to achieve the same functionality using local notifications.

You can use background execution if you need to execute tasks while your app is not in the foreground, but if you only need your tasks to run while your app is in the foreground, there are better ways to achieve this than using notifications. Even though your question is quite vague about what you actually want to achieve, you mention session timeout. For measuring session timeout, you can just use ordinary Date objects, save the current date when you start the session and check the elapsed time using Date().timeIntervalSince(date: startDate) before you would need to use your token to decide whether the token has expired or not.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • 1
    You need a timer to know WHEN to check the date time interval. What I was hoping was to be able to use the notification as a trigger to tell me when to check it. Thanks for your answer. I am going to put in a request to Apple to enhance it to allow it as they do foresee an app handling the notification without user intervention when the app is running by the virtue of the completionHandler on the delegate routine that processes the incoming notification being able to take "none" as a parameter. – chadbag Sep 01 '17 at 15:19
  • @chadbag How did u achieve it finally? – iAnurag Jun 14 '18 at 08:17
  • @iAnurag. We changed the requirements to not need to check until the app actually runs instead of forcing the app to run at the timer expiration time. – chadbag Jul 03 '18 at 01:33