2014-10-13 3 views
0

В настоящее время у меня есть служба, которая играет музыку. Я хочу показать уведомление, когда служба начнет воспроизводить музыку. Это то, что я делаю:Android показать уведомление не работает

public void setUpNotification() { 
    /* 
    * Set up the notification for the started service 
    */ 

    Notification.Builder notifyBuilder = new Notification.Builder(this); 
    notifyBuilder.setTicker("ticker"); 
    notifyBuilder.setOngoing(true); 
    notifyBuilder.setContentTitle("title"); 
    notifyBuilder.setContentInfo("content info"); 

    // set up an Intent if the user clicks the notification 
    int NOTIFICATION_ID = 1; 
    Intent notifyIntent = new Intent(this, MainActivity.class); 

    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent pi = PendingIntent.getActivity(this, 0, notifyIntent, 
      NOTIFICATION_ID); 

    notifyBuilder.setContentIntent(pi); 
    Notification notification = notifyBuilder.build(); 
    // start ongoing 
    startForeground(NOTIFICATION_ID, notification); 
} 

метод вызывается в службе playMusic(), как это:

public void playMusic(String songPath) { 
    if(mPlayer == null || !mPlayer.isPlaying()){ 
     try { 
      mPlayer = MediaPlayer.create(getBaseContext(), Uri.parse(songPath)); 
      mPlayer.start(); 
      this.songPath = songPath; 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } 
    }else{ 
     mPlayer.stop(); 
     mPlayer.release(); 
     mPlayer = MediaPlayer 
       .create(getBaseContext(), Uri.parse(songPath)); 
     mPlayer.start(); 
     this.songPath = songPath; 
    } 
    setUpNotification(); 
} 

Проблема заключается в том, что уведомления в ожидании намерения, название контента и информации контента не получают отображается. Уведомление выглядит так: screenshot

И как вы можете видеть, информация о названии и содержании не отображается. Кроме того, когда я нажимаю уведомление, оно не отменяет мои ожидающие намерения.

Что я здесь делаю неправильно? Любая помощь приветствуется.

Маркус

ответ

2

Пер с notification guide list of required fields, вы должны установить небольшую иконку с помощью метода setSmallIcon(). Как правило, этот значок выглядит как ваш значок приложения, хотя он полностью белый на прозрачном фоне на notification iconography guide.

+0

Спасибо, связка, это решило это. Я понятия не имел, что значок нужен. Оценил! – Marcus

Смежные вопросы