2017-12-30 16 views
0

私はAndroid Oに私のアプリをサポートしたいと思っています。そして、私はちょうどこれに似た色付き通知という概念が大好きです。私は画像の下のように達成したいAndroid Oreo以上のデバイスで通知を色分けする方法は?

import android.app.Notification; 
import android.app.NotificationChannel; 
import android.app.NotificationManager; 
import android.os.Build; 
import android.support.annotation.RequiresApi; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 
Button bt; 
String channelID="channelID"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     bt=findViewById(R.id.button); 
     bt.setOnClickListener(new View.OnClickListener() { 
      @RequiresApi(api = Build.VERSION_CODES.O) 
      @Override 
      public void onClick(View view) { 
       Notification notification=new Notification.Builder(MainActivity.this) 
         .setContentTitle("This is an title") 
         .setContentText("This is an long text") 
         .setChannelId(channelID) 
         .setOngoing(true) 
         .setColor(getResources().getColor(R.color.blue)) 
         .setColorized(true) 
         .setSmallIcon(R.drawable.ic_launcher_foreground) 
         .build(); 
       NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
       NotificationChannel channel= null; 
       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 
        channel = new NotificationChannel(channelID,"Popup notification channel", NotificationManager.IMPORTANCE_HIGH); 
        channel.setLightColor(getResources().getColor(R.color.blue)); 
        channel.setShowBadge(true); 
        manager.createNotificationChannel(channel); 
       } 
       manager.notify(0,notification); 
      } 
     }); 
    } 
} 

答えて

0

をあなたはNotificationCompat.Builderを使用して、MediaStyle]によって設定する必要があります。

enter image description here

ここでは、私が現在やっているものです。

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
    .setStyle(new NotificationCompat.MediaStyle() 
       // Attach our MediaSession token 
      .setMediaSession(mediaSession.getSessionToken()) 
      // Show our playback controls in the compat view 
      .setShowActionsInCompactView(0, 1, 2)) 

今実行して、チェックアンドロイドOで

+0

私は任意の通知スタイルなしで行うことができます????? –

+0

いいえあなたは通知スタイル –

+0

高齢者を使用する必要はありません、あなたは私の答えplzでこの問題を閉じるsatisfay場合。 –

関連する問題