iOS Question UserNotificationCenter - Trigger notification without alert

Alexander Stolte

Expert
Licensed User
Longtime User

I would like to display a notification without the popup appearing (without an alert). According to Apple's documentation, I can do this with the “UNNotificationPresentationOptionNone” option. How can I use this with the UserNotificationCenter class?


From the UserNotificationCenter class:
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))
    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

#if OBJC
#import <UserNotifications/UserNotifications.h>
@end
@interface b4i_usernotificationcenter (notification) <UNUserNotificationCenterDelegate>
@end
@implementation b4i_usernotificationcenter (notification)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
        completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound );
   }
 - (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler {
       B4I* bi = [b4i_main new].bi;
[bi raiseEvent:nil event:@"usernotification_action:" params:@[response]];
completionHandler();
   }
#End If

Thanks :)
 
Solution
This is the solution:
B4X:
completionHandler(UNNotificationPresentationOptionNone | UNNotificationPresentationOptionList );
we need to add UNNotificationPresentationOptionList too.

btw: The UNNotificationPresentationOptionAlert is Deprecated since ios 14

Alexander Stolte

Expert
Licensed User
Longtime User
Thank you very much, but somehow it doesn't seem to work. No notification is displayed in the notification center. Or am I misunderstanding the function?
B4X:
#if OBJC
#import <UserNotifications/UserNotifications.h>
@end
@interface b4i_usernotificationcenter (notification) <UNUserNotificationCenterDelegate>
@end
@implementation b4i_usernotificationcenter (notification)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
        completionHandler(UNNotificationPresentationOptionNone);
   }
 - (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler {
       B4I* bi = [b4i_main new].bi;
[bi raiseEvent:nil event:@"usernotification_action:" params:@[response]];
completionHandler();
   }
#End If
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
This is the solution:
B4X:
completionHandler(UNNotificationPresentationOptionNone | UNNotificationPresentationOptionList );
we need to add UNNotificationPresentationOptionList too.

btw: The UNNotificationPresentationOptionAlert is Deprecated since ios 14
 
Upvote 1
Solution
Top