iOS Code Snippet UserNotificationCenter class - Time sensitive notifications


This is an addition to the UserNotificationCenter class.

I need a notification that is displayed on the lock screen and is not immediately pushed to the notification center. You can do this by declaring a notification as “time critical” via the UNNotificationInterruptionLevel.


Thanks to @Erel for his answer here:

Steps
  1. Go to the Apple developer console
  2. Open the Identifiers
  3. Open the one from your app
  4. Scroll down to "Time Sensitive Notifications" and check this option
  5. save it and update your mobileprovision file from the app and download it


6. Add this line to the Project Attributes in the main module of your app
B4X:
#Entitlement: <key>com.apple.developer.usernotifications.time-sensitive</key><true/>
7. Open the "UserNotificationCenter" class and add the following line to the CreateNotificationWithContent sub
B4X:
Public Sub CreateNotificationWithContent(Title As String, Body As String, Identifier As String, Category As String, MillisecondsFromNow As Long)
    Dim ln As NativeObject
    ln = ln.Initialize("UNMutableNotificationContent").RunMethod("new", Null)
    ln.SetField("title", Title)
    ln.SetField("body", Body)
    ln.SetField("interruptionLevel", 2) 'Add this line - 2 = UNNotificationInterruptionLevelTimeSensitive
    Dim n As NativeObject
    ln.SetField("sound", n.Initialize("UNNotificationSound").RunMethod("defaultSound", Null))
    If Category <> "" Then ln.SetField("categoryIdentifier", Category)
    Dim trigger As NativeObject
    trigger = trigger.Initialize("UNTimeIntervalNotificationTrigger").RunMethod("triggerWithTimeInterval:repeats:", Array(MillisecondsFromNow / 1000, False))
    Dim request As NativeObject
    request = request.Initialize("UNNotificationRequest").RunMethod("requestWithIdentifier:content:trigger:", _
       Array(Identifier, ln, trigger))
    Dim NotificationCenter As NativeObject
    NotificationCenter = NotificationCenter.Initialize("UNUserNotificationCenter").RunMethod("currentNotificationCenter", Null)
    NotificationCenter.RunMethod("addNotificationRequest:", Array(request))
End Sub

8. Done

Open the app and the following item should now be available in the app settings on the iPhone:



The notification is marked as "Time sensitive" and is now visible at the lockscreen for 1 hour
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…