iOS Question UserNotificationCenter - set interruptionLevel for time sensitive notifications

Alexander Stolte

Expert
Licensed User
Longtime User
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 returns an Object was not initialized (NSObject) error.


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)
    Dim n As NativeObject
    ln.SetField("sound", n.Initialize("UNNotificationSound").RunMethod("defaultSound", Null))
    Dim n2 As NativeObject
    ln.SetField("interruptionLevel", n2.Initialize("UNNotificationInterruptionLevel").RunMethod("UNNotificationInterruptionLevelTimeSensitive", Null)) 'Error - Object was not initialized (NSObject)
    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

Thanks
 
Solution
Enums are constants and you need to do some work to get their actual value:
B4X:
#if OBJC
 #import <UserNotifications/UserNotifications.h>
-(void) test {
    NSLog(@"%@", @(UNNotificationInterruptionLevelTimeSensitive));
}
#End If

B4X:
 ln.SetField("interruptionLevel", 2) 'UNNotificationInterruptionLevelTimeSensitive

Erel

B4X founder
Staff member
Licensed User
Longtime User
Enums are constants and you need to do some work to get their actual value:
B4X:
#if OBJC
 #import <UserNotifications/UserNotifications.h>
-(void) test {
    NSLog(@"%@", @(UNNotificationInterruptionLevelTimeSensitive));
}
#End If

B4X:
 ln.SetField("interruptionLevel", 2) 'UNNotificationInterruptionLevelTimeSensitive
 
Last edited:
Upvote 1
Solution

Alexander Stolte

Expert
Licensed User
Longtime User
All steps summarized in a separate thread:
 
Upvote 0
Top