iOS Tutorial Notifications with actions

Discussion in 'iOS Tutorials' started by Erel, Dec 20, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User


    It is possible to add a list of actions to the push notifications. The list becomes visible when the user 3d touches the notification or drags it with two fingers.
    Note that it is an iOS 10+ feature.

    Before you start make sure to first implement the standard push notifications and see that they work with the B4J code.

    Steps to add actions:

    1. Add this code at the end of the main module:
    #AdditionalLib: UserNotifications.framework
    #if OBJC
    #import <UserNotifications/UserNotifications.h>
    @interface b4i_main (notification) <UNUserNotificationCenterDelegate>
    @implementation b4i_main (notification)
    - (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]];
    #End If
    Define the list of actions in SetCategories. Call SetCategories from ApplicationStart.

    Sub SetCategories
    Dim category As NativeObject
       category = category.Initialize(
    'create a list with the action identifiers and titles: <--------------------------------------------
       Dim actions As List = Array(CreateAction("a1""Action 1"), CreateAction("a2""Action 2"))
    Dim intentIdentifiers As List = Array()
       category = category.RunMethod(
    "categoryWithIdentifier:actions:intentIdentifiers:options:", _
    Array("category 1", actions, intentIdentifiers, 1))
    Dim NotificationCenter As NativeObject
       NotificationCenter = NotificationCenter.Initialize(
    Dim set As NativeObject
       set = set.Initialize(
    "delegate", Me)
    End Sub

    Sub CreateAction (Identifier As String, Title As StringAs Object
    Dim acceptAction As NativeObject
    '5 = AuthenticationRequired + Foreground
       acceptAction = acceptAction.RunMethod("actionWithIdentifier:title:options:"Array(Identifier, Title, 5))
    Return acceptAction
    End Sub

    Private Sub UserNotification_Action (Response As Object)
    Dim n As NativeObject = Response
    Dim ActionIdentifier As String = n.GetField("actionIdentifier").AsString
    Msgbox($"User clicked on : ${ActionIdentifier}"$"")
    End Sub
    Step 3: B4J code!
    Set the category in the push notification json message:
    Dim data As Map = CreateMap("title": Title, "body": Body)
    If Topic.StartsWith("ios_"Then
    Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound""default")
    "click_action""category 1"'<--------------------------------------
           m.Put("notification", iosalert)
    End If
    UserNotification_Action event will be called when the user pressed on an action.
  2. MarcoRome

    MarcoRome Expert Licensed User

    Big Master :D:D
  3. Sandman

    Sandman Well-Known Member Licensed User

    But surely these actions also work with local notifications?
  4. Erel

    Erel Administrator Staff Member Licensed User

    Create a local notification with action items:
    Sub Page1_Click
    "This is the title""Body""identifer 2""category 1"5000)
    End Sub

    Sub CreateNotificationWithContent(Title As String, Body As String, Identifier As String, Category As String, MillisecondsFromNow As Long)
    Dim ln As NativeObject
       ln = ln.Initialize(
    "title", Title)
    "body", Body)
    Dim n As NativeObject
    "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 / 1000False))
    Dim request As NativeObject
       request = request.Initialize(
    "UNNotificationRequest").RunMethod("requestWithIdentifier:content:trigger:", _
    Array(Identifier, ln, trigger))
    Dim NotificationCenter As NativeObject
       NotificationCenter = NotificationCenter.Initialize(
    End Sub
    MarcoRome, Sandman and Jamie8763 like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice