Notification wird nicht angezeigt

Q

qwert_zuiopue

Fortgeschrittenes Mitglied
6
Hallo :),

ich habe jetzt schon einiges ausprobiert und auch aus diversen Foren und Anleitungen im Internet Kopiert, aber egal wie ich es auch mache, es wird einfach keine Notification angezeigt.

Folgender Code (zusammenkopiert ;) ):
Code:
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;

public class NotificationService extends Service {
	private static int num_msg = 0;
	public static void incNumMsg() {
		num_msg++;
	}
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }	 
    @Override
    public void onCreate() {
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Query the database and show alarm if it applies
    	Datenbank.getInstance().update();
    	int tmp = Datenbank.getInstance().getNumOfMessagesForUser(Datenbank.getInstance().getMyID());
    	System.out.println("Service meldet: "+tmp);
		NotificationCompat.Builder builder =
	            new NotificationCompat.Builder(this)
	                    .setContentTitle("My Notification Title")
	                    .setContentText("Something interesting happened");
	    int NOTIFICATION_ID = 12345;

	    Intent targetIntent = new Intent(this, TargetActivity.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());
		
	
        // I don't want this service to stay in memory, so I stop it
        // immediately after doing what I wanted it to do.
        stopSelf();

        return Service.START_STICKY;
    }
    @Override
    public void onDestroy() {
        // I want to restart this service again in one hour
        AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarm.set(
            AlarmManager.RTC,
            System.currentTimeMillis() + (1000 * 5),
            PendingIntent.getService(this, 0, new Intent(this, NotificationService.class), 0)
        );
    }
}

wird aufgerufen. Die Ausgabe "Service meldet: X" erscheint auch, aber es wird eben keine Notification angezeigt.

Was mache ich hier falsch?

Vielen Dank :)!
 
Gibt meiner Meinung nach nur zwei Möglichkeiten:
1. der Service geht direkt wieder in die Knie wg. stopSelf()...
2. die Benachrichtigung geht verloren
probiere mal
Code:
  NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(title)
                        .setContentText(message)
                        .setOngoing(true)
                        .setAutoCancel(false);

Vielleicht ist es auch sinnvoller einen Teil deiner Initialisierung in onCreate auszulagern (weiß nicht genau wie hier Android die Ressourcen verteilt)...
 
  • Danke
Reaktionen: qwert_zuiopue
Tatsächlich, so geht's. Vielen Dank :).
 
  • Danke
Reaktionen: ui_3k1

Ähnliche Themen

M
  • MikelKatzengreis
Antworten
10
Aufrufe
240
swa00
swa00
SaniMatthias
Antworten
19
Aufrufe
986
swa00
swa00
L
Antworten
17
Aufrufe
1.217
jogimuc
J
Zurück
Oben Unten