Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a notification from a service in Android

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.

like image 987
e-satis Avatar asked Jul 30 '09 15:07

e-satis


People also ask

How do I set up notification services on Android?

To create notifications you use the NotificationManager class which can be received from the Context , e.g. an activity or a service, via the getSystemService() method. NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); The Notification.

How do I send notifications from a website to my Android app?

For sending Notification to any android Device you can use two technology: 1) Push. 2) Pull. For Push Technology you can use GCM(Google cloud messaging). For Pull Technology you can make you application continuously keep on connecting to server and trying to fetch data if it is available from there.

How do I start a service from notification?

This example demonstrate about How to start a service from notification in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

Both Activity and Service actually extend Context so you can simply use this as your Context within your Service.

NotificationManager notificationManager =     (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE); Notification notification = new Notification(/* your notification */); PendingIntent pendingIntent = /* your intent */; notification.setLatestEventInfo(this, /* your content */, pendingIntent); notificationManager.notify(/* id */, notification); 
like image 111
Josef Pfleger Avatar answered Sep 23 '22 12:09

Josef Pfleger


This type of Notification is deprecated as seen from documents:

@java.lang.Deprecated public Notification(int icon, java.lang.CharSequence tickerText, long when) { /* compiled code */ }  public Notification(android.os.Parcel parcel) { /* compiled code */ }  @java.lang.Deprecated public void setLatestEventInfo(android.content.Context context, java.lang.CharSequence contentTitle, java.lang.CharSequence contentText, android.app.PendingIntent contentIntent) { /* compiled code */ } 

Better way
You can send a notification like this:

// prepare intent which is triggered if the // notification is selected  Intent intent = new Intent(this, NotificationReceiver.class); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);  // build notification // the addAction re-use the same intent to keep the example short Notification n  = new Notification.Builder(this)         .setContentTitle("New mail from " + "[email protected]")         .setContentText("Subject")         .setSmallIcon(R.drawable.icon)         .setContentIntent(pIntent)         .setAutoCancel(true)         .addAction(R.drawable.icon, "Call", pIntent)         .addAction(R.drawable.icon, "More", pIntent)         .addAction(R.drawable.icon, "And more", pIntent).build();   NotificationManager notificationManager =    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  notificationManager.notify(0, n);  

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:

    import android.support.v4.app.NotificationCompat;     -------------     NotificationCompat.Builder builder =             new NotificationCompat.Builder(this)                     .setSmallIcon(R.drawable.mylogo)                     .setContentTitle("My Notification Title")                     .setContentText("Something interesting happened");     int NOTIFICATION_ID = 12345;      Intent targetIntent = new Intent(this, MyFavoriteActivity.class);     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);     builder.setContentIntent(contentIntent);     NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);     nManager.notify(NOTIFICATION_ID, builder.build()); 
like image 37
trante Avatar answered Sep 22 '22 12:09

trante