UserNotificationCenter class
A class that creates local notifications based on the newer iOS 10+ API. The notifications are configured to show even when the app is in the foreground. You can also add actions to the notifications: See the attached example
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.
UNNotificationInterruptionLevel | Apple Developer Documentation
Constants that indicate the importance and delivery timing of a notification.
UNNotificationInterruptionLevel.timeSensitive | Apple Developer Documentation
The system presents the notification immediately, lights up the screen, can play a sound, and breaks through system notification controls.
Thanks to @Erel for his answer here:
UserNotificationCenter - set interruptionLevel for time sensitive notifications
In my app, 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”. Now I don't know how to integrate the whole thing correctly with Native Object. My following code...
Steps
- Go to the Apple developer console
- Open the Identifiers
- Open the one from your app
- Scroll down to "Time Sensitive Notifications" and check this option
- 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/>
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: