Android Code Snippet Notification with reply field

Erel

B4X founder
Staff member
Licensed User
Longtime User
1619332462755.png


1. Add to NB6 class:
B4X:
Public Sub AddRemoteInput (Bmp As Bitmap, Title As Object, ResultKey As String, Service As Object) As NB6
    If IsBuilder = False Then Return Me
    Dim RemoteInputBuilder As JavaObject
    RemoteInputBuilder.InitializeNewInstance("android.app.RemoteInput$Builder", Array(ResultKey))
    RemoteInputBuilder.RunMethod("setLabel", Array(Title))
    Dim builder As JavaObject
    builder.InitializeNewInstance("android.app.Notification$Action$Builder", Array(CreateIconFromBitmap(Bmp), Title, CreateReceiverPendingIntent(Service, ResultKey)))
    builder.RunMethod("addRemoteInput", Array(RemoteInputBuilder.RunMethod("build", Null)))
    Dim ac As JavaObject = builder.RunMethod("build", Null)
    NotificationBuilder.RunMethod("addAction", Array(ac))
    Return Me
End Sub

2. Create a notification and call AddRemoteInput:
B4X:
Dim n As NB6
    n.Initialize("default", Application.LabelName, "HIGH").AutoCancel(True).SmallIcon(smiley)
    n.AddRemoteInput(smiley, "Text", "remote input", MyService) 'MyService = service that will accept the reply
    n.Build("Title", "Content", "tag1", Main).Notify(Starter.ReplyIntentId)
The notification id (Starter.ReplyIntentId in this case) will be used later to accept the reply.

3. Service code:
B4X:
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then
        If StartingIntent.Action = "remote input" Then
            Dim RemoteInput As JavaObject
            Dim bundle As JavaObject = RemoteInput.InitializeStatic("android.app.RemoteInput").RunMethod("getResultsFromIntent", Array(StartingIntent))
            If bundle.IsInitialized Then
                Dim Input As String = bundle.RunMethod("getCharSequence", Array("remote input"))
                Log(Input) 'input string
                Dim notif As Notification
                notif.Initialize
                notif.Icon = "icon"
                notif.SetInfo("Reply accepted", "", Main)
                notif.Notify(Starter.ReplyIntentId)
            End If
        End If
    End If
    Service.StopAutomaticForeground 
End Sub
 

ALISSON P C SILVA

Member
Licensed User
View attachment 112243

1. Add to NB6 class:
B4X:
Public Sub AddRemoteInput (Bmp As Bitmap, Title As Object, ResultKey As String, Service As Object) As NB6
    If IsBuilder = False Then Return Me
    Dim RemoteInputBuilder As JavaObject
    RemoteInputBuilder.InitializeNewInstance("android.app.RemoteInput$Builder", Array(ResultKey))
    RemoteInputBuilder.RunMethod("setLabel", Array(Title))
    Dim builder As JavaObject
    builder.InitializeNewInstance("android.app.Notification$Action$Builder", Array(CreateIconFromBitmap(Bmp), Title, CreateReceiverPendingIntent(Service, ResultKey)))
    builder.RunMethod("addRemoteInput", Array(RemoteInputBuilder.RunMethod("build", Null)))
    Dim ac As JavaObject = builder.RunMethod("build", Null)
    NotificationBuilder.RunMethod("addAction", Array(ac))
    Return Me
End Sub

2. Create a notification and call AddRemoteInput:
B4X:
Dim n As NB6
    n.Initialize("default", Application.LabelName, "HIGH").AutoCancel(True).SmallIcon(smiley)
    n.AddRemoteInput(smiley, "Text", "remote input", MyService) 'MyService = service that will accept the reply
    n.Build("Title", "Content", "tag1", Main).Notify(Starter.ReplyIntentId)
The notification id (Starter.ReplyIntentId in this case) will be used later to accept the reply.

3. Service code:
B4X:
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then
        If StartingIntent.Action = "remote input" Then
            Dim RemoteInput As JavaObject
            Dim bundle As JavaObject = RemoteInput.InitializeStatic("android.app.RemoteInput").RunMethod("getResultsFromIntent", Array(StartingIntent))
            If bundle.IsInitialized Then
                Dim Input As String = bundle.RunMethod("getCharSequence", Array("remote input"))
                Log(Input) 'input string
                Dim notif As Notification
                notif.Initialize
                notif.Icon = "icon"
                notif.SetInfo("Reply accepted", "", Main)
                notif.Notify(Starter.ReplyIntentId)
            End If
        End If
    End If
    Service.StopAutomaticForeground
End Sub

Is it necessary to declare the variable "ReplyIntentId" in the module "Starter"?
 
Top