Android Question Click on a notification from a service

laviniut

Active Member
Licensed User
Longtime User
I have a service that runs in background. When a notification starts from that service, i want to know when the notification is clicked in notification bar. I saw setinfo2 with tag, but i don't want to call in foreground an activity but i want to run a code when notification is clicked. How can i do that ?
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
The only solution I know (I am not an expert) is to show a transparent activity when the notification is pressed. In its Activity_Resume I would make the call to the service's sub and close the activity.
 
Upvote 0

Danbicky

Member
Licensed User
Longtime User
Hi @laviniut, I would be very grateful if you could post an example of your service and interface method to an activity once clicked. Would allow me to learn some basic service functionality.

Regards

Dans
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Main
B4X:
    StopService(mysvc)
    CancelScheduledService(mysvc)
    StartService(mysvc)

MySvc
B4X:
Sub Service_Start (StartingIntent As Intent)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo2("This is the title", "and this is the body.", "PrintToLog", notificationreceiver)
    n.Notify(1)

End Sub

Sub PrintToLog()
    Log("Notification was clicked and the given sub was called. This one PrintToLog() in the Service")
End Sub

notificationreceiver activity
B4X:
Sub Activity_Resume
  Dim in As Intent
  in = Activity.GetStartingIntent
  If in.HasExtra("Notification_Tag") Then
    Dim notifStr As String = in.GetExtra("Notification_Tag")
        'Log(in.GetExtra("Notification_Tag")) 'Will log the tag
        If notifStr <> "" Then
            CallSubDelayed(mysvc,notifStr)
            Activity.Finish
        End If
  End If
End Sub

** Activity (main) Pause, UserClosed = true **
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (mysvc) Create **
** Service (mysvc) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (notificationreceiver) Create, isFirst = true **
** Activity (notificationreceiver) Resume **
Notification was clicked and the given sub was called. This one PrintToLog() in the Service
** Activity (notificationreceiver) Pause, UserClosed = true **
 

Attachments

  • ServiceNotification.zip
    8 KB · Views: 327
Upvote 0

laviniut

Active Member
Licensed User
Longtime User
Thank you DonManfred for your example, but is not what i need. I have a service that monitors battery voltage and when battery is charged or battery is discharged, the service make a notification repeatedly until charger is connected or disconnected. I want to stop the notification in the service running in background when the user click on the notification in notification bar.
 
Upvote 0

laviniut

Active Member
Licensed User
Longtime User
Idea from lemonisdead is good but i read about transparent activity and i think is not so easy to make it (to make a single activity transparent and not the hole application).
 
Upvote 0
Top