Android Question Notofication and Service

Discussion in 'Android Questions' started by Nickelgrass, Jun 9, 2019.

  1. Nickelgrass

    Nickelgrass Active Member Licensed User

    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)?
    Best regards
  2. wes58

    wes58 Active Member Licensed User

    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.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Yes. See NB6 example (Notification_WithActions).
  4. wes58

    wes58 Active Member Licensed User

    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
    '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
    Sub Build (ContentTitle As Object, ContentText As Object, Tag As StringActivity As Object, Service As StringAs Notification
    If IsOld Then
            OldNotification.SetInfo2(ContentTitle, ContentText, Tag, 
    Return OldNotification
    If Not(IsChannel) And nDefaults <> 7 Then        'not all true
                Log("nDefaults = " & nDefaults)
    End If
    If Activity <> Null Then
    Dim in As Intent = CreateIntent(ActivityFalse)
                in.PutExtra("Notification_Tag", Tag)
    Dim PendingIntent As Object = PendingIntentStatic.RunMethod("getActivity"Array(ctxt, Rnd(00x7fffffff), in0))
    'added for service
            else If Service <> "" And Tag <> "" Then
    Dim srvIn As Intent
    Application.PackageName & "/." & Service.ToLowerCase)
    "Notification_Tag", Tag)
    Dim SrvPendingIntent As Object = PendingIntentStatic.RunMethod("getService"Array(ctxt, 1, srvIn, 1073741824))
    End If
    If IsChannel Then
    Dim manager As JavaObject = ctxt.RunMethod("getSystemService"Array("notification"))
    If NotifChannelGroup.IsInitialized Then
    End If
    End If
    Return NotificationBuilder.RunMethod("build"Null)
    End If
    End Sub
    Create notification:
    Public Sub CreateNotification   
    Dim n As NB6
        add code 
    To setup CreateNotification
    "Title""Content Text""Tag1"Null"YourServiceModuleName").Notify(100)
    End Sub
    In Service_Start filter the tag:
    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
  5. Nickelgrass

    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!
  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