Invio di una notifica da un servizio in Android


105

Ho un servizio in esecuzione e vorrei inviare una notifica. Peccato, l'oggetto di notifica richiede un Context, come un Activitye non un Service.

Conosci un modo per aggirarlo? Ho provato a creare un messaggio Activityper ogni notifica, ma sembra brutto e non riesco a trovare un modo per avviarne uno Activitysenza View.


14
Umm ... un servizio è un contesto!
Isaac Waller

19
Dio, sono un tale manubrio. Ok, scusa se ho perso tempo a tutti.
e-satis

28
Va bene, è una buona domanda di Google.
Isaac Waller

Come il tuo secondo commento: D: D
Faizan Mubasher

Questo post mi ha appena salvato la giornata ...
Muhammad Faizan

Risposte:


109

Entrambi Activitye in Servicerealtà extend Contextcosì puoi semplicemente usare thiscome tuo Contextdentro il tuo 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);

4
Tieni presente che avrai molti problemi con le notifiche da un servizio. Se hai problemi, dai un'occhiata a questo groups.google.com/group/android-developers/browse_thread/thread/…
Karussell

1
come puoi farlo usando Notification.Builder? perché setLatestEventInfo è già deprecato.
Kairi San

77

Questo tipo di notifica è deprecato come visto dai documenti:

@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 */ }

Modo migliore
Puoi inviare una notifica come questa:

// 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 " + "test@gmail.com")
        .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); 

Modo migliore Il
codice precedente richiede il livello API minimo 11 (Android 3.0).
Se il tuo livello API minimo è inferiore a 11, dovresti utilizzare la classe NotificationCompat della libreria di supporto in questo modo.

Quindi, se il tuo livello API target minimo è 4+ (Android 1.6+), utilizza questo:

    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());

4
questa dovrebbe essere la risposta migliore poiché quella accettata è deprecata
Marcel Krivek

4
@MarcelKrivek Sembra che si sia "dimenticato" di citare la loro fonte. vogella.com/tutorials/AndroidNotifications/article.html
StarWind0

Che cos'è "NotificationReceiver"?
user3690202

"NotificationReceiver" è l'attività che verrà aperta dalla notifica. Controllare il collegamento fornito da @ StarWind0.
George Theodorakis

NotificationCompat.Builder già deprecato. Ora non è più la risposta migliore
Devil's Dream

7
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void PushNotification()
{
    NotificationManager nm = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(context);
    Intent notificationIntent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,0);

    //set
    builder.setContentIntent(contentIntent);
    builder.setSmallIcon(R.drawable.cal_icon);
    builder.setContentText("Contents");
    builder.setContentTitle("title");
    builder.setAutoCancel(true);
    builder.setDefaults(Notification.DEFAULT_ALL);

    Notification notification = builder.build();
    nm.notify((int)System.currentTimeMillis(),notification);
}

La domanda dell'argomento proviene da un SERVIZIO non attività
Duna

1

Beh, non sono sicuro che la mia soluzione sia la migliore pratica. L'utilizzo del NotificationBuildermio codice ha questo aspetto:

private void showNotification() {
    Intent notificationIntent = new Intent(this, MainActivity.class);

    PendingIntent contentIntent = PendingIntent.getActivity(
                this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
    }

Manifesto:

    <activity
        android:name=".MainActivity"
        android:launchMode="singleInstance"
    </activity>

e qui il Servizio:

    <service
        android:name=".services.ProtectionService"
        android:launchMode="singleTask">
    </service>

Non so se esiste davvero un singleTaskat Servicema funziona correttamente nella mia applicazione ...


qual è il costruttore in questo?
Viswanath Lekshmanan

-2

Se nessuno di questi funziona, prova getBaseContext(), invece di contexto this.


2
Non dovresti usare getBaseContext()questi scenari.
Rahat Zaman
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.