I have a service running, and would like to send a notification. Too bad, the notification object requires a Context
, like an Activity
, and not a Service
.
Do you know any way to by pass that ? I tried to create an Activity
for each notification but it seems ugly, and I can't find a way to launch an Activity
without any View
.
Well, I'm not sure if my solution is best practice. Using the
NotificationBuilder
my code looks like that:Manifest:
and here the Service:
I don't know if there really is a
singleTask
atService
but this works properly at my application...This type of Notification is deprecated as seen from documents:
Better way
You can send a notification like this:
Best way
Code above needs minimum API level 11 (Android 3.0).
If your minimum API level is lower than 11, you should you use support library's NotificationCompat class like this.
So if your minimum target API level is 4+ (Android 1.6+) use this:
Both
Activity
andService
actuallyextend
Context
so you can simply usethis
as yourContext
within yourService
.If none of these work, try
getBaseContext()
, instead ofcontext
orthis
.