Android Question Notofication and Service

Nickelgrass

Active Member
Licensed User
Hi,
is it possible to get the click event of a notification in a service module?
I would like to show a notification. If it is clicked it should disapear and a service module should react to it without showing an activity.
Does it work somehow nicley or do I have to make a dummy activity that is opend, calls a service sub and then finishes itself (somehow not nice)?
Thanks
Best regards
 

wes58

Active Member
Licensed User
Hi,
is it possible to get the click event of a notification in a service module?
I would like to show a notification. If it is clicked it should disapear and a service module should react to it without showing an activity.
Does it work somehow nicley or do I have to make a dummy activity that is opend, calls a service sub and then finishes itself (somehow not nice)?
Thanks
Best regards
A short reply is - yes you can. You have to create a PendingIntent using PendingIntent.getService.
But because you didn't say how you sending notifications - i.e what library you are using - I can't do anymore.
 

wes58

Active Member
Licensed User
Yes. See NB6 example (Notification_WithActions).
I guess, that he has to be more specific what he wants. I understood that he wanted a response on the click on the notification - not on a button placed on the notification. Similar to launching an activity when the user clicks on the notification. That's why I wrote that he needed pending intent.

That's what I used in NB6 class - modified sub Build, added another parameter for service name
B4X:
'Build the notification and returns the notification object.
'ContentTitle - Title (CharSequence)
'ContentText - Body text (CharSequence)
'Tag - Tag that can be intercepted in Activity_Resume (or Service_Start) when the user clicks on the notificaiton.
'Activity - The activity that will be launched when the user clicks on the notification. Set to null if not used
'Service - The service module name that will receive notification when the user clicks on the notification. Set to "" if not used
Public 
Sub Build (ContentTitle As Object, ContentText As Object, Tag As String, Activity As Object, Service As String) As Notification
    If IsOld Then
        OldNotification.SetInfo2(ContentTitle, ContentText, Tag, Activity)
        Return OldNotification
    Else
        If Not(IsChannel) And nDefaults <> 7 Then        'not all true
            Log("nDefaults = " & nDefaults)
            NotificationBuilder.RunMethod("setDefaults", Array(nDefaults))
        End If
        If Activity <> Null Then
            Dim in As Intent = CreateIntent(Activity, False)
            in.Flags = Bit.Or(268435456, 131072) 'FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_REORDER_TO_FRONT
            in.PutExtra("Notification_Tag", Tag)
            Dim PendingIntent As Object = PendingIntentStatic.RunMethod("getActivity", Array(ctxt, Rnd(0, 0x7fffffff), in, 0))
            NotificationBuilder.RunMethod("setContentIntent", Array(PendingIntent))
        'added for service
        else If Service <> "" And Tag <> "" Then
            Dim srvIn As Intent
            srvIn.Initialize("", "")
            srvIn.SetComponent(Application.PackageName & "/." & Service.ToLowerCase)
            srvIn.PutExtra("Notification_Tag", Tag)
            Dim SrvPendingIntent As Object = PendingIntentStatic.RunMethod("getService", Array(ctxt, 1, srvIn, 1073741824))
            NotificationBuilder.RunMethod("setContentIntent", Array(SrvPendingIntent))
        End If
        
        NotificationBuilder.RunMethodJO("setContentTitle", Array(ContentTitle)).RunMethodJO("setContentText", Array(ContentText))
        If IsChannel Then
            Dim manager As JavaObject = ctxt.RunMethod("getSystemService", Array("notification"))
            
            If NotifChannelGroup.IsInitialized Then
                manager.RunMethod("createNotificationChannelGroup", Array(NotifChannelGroup))
                Channel.RunMethod("setGroup", Array(groupID))
            End If
            manager.RunMethod("createNotificationChannel", Array(Channel))       
        End If
        Return NotificationBuilder.RunMethod("build", Null)
    End If
End Sub
Create notification:
B4X:
Public Sub CreateNotification   
    Dim n As NB6
    
    ...
    add code To setup CreateNotification
    ...
    n.Build("Title", "Content Text", "Tag1", Null, "YourServiceModuleName").Notify(100)
End Sub
In Service_Start filter the tag:
B4X:
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.HasExtra("Notification_Tag") Then    'notification clicked
        If StartingIntent.GetExtra("Notification_Tag") = "Tag1" Then
            Log("1 Notification click with tag = " & StartingIntent.GetExtra("Notification_Tag"))
        else If StartingIntent.GetExtra("Notification_Tag") = "Tag2" Then
            Log("2 Notification click with tag = " & StartingIntent.GetExtra("Notification_Tag"))
        End If
    End If
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    
End Sub
 

Nickelgrass

Active Member
Licensed User
Thank you all for the rplies.
I wanted to do exactly what you explained. When I click the notification I wanted to inform a service instead of starting an activity. I will try your code. Thanks!
 
Top