Android Question Notofication and Service

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

  1. Nickelgrass

    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
     
  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
    Code:
    '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 StringActivity As Object, Service As StringAs 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(ActivityFalse)
                
    in.Flags = Bit.Or(268435456131072'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(00x7fffffff), in0))
                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:
    Code:
    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:
    Code:
    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!
     
Loading...
  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