I just want to support my app to Android O. And I just love the concept called colorized notification which is similar to this.
I want to achieve just like below image:
Here is what I am currently doing:
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);
}
});
}
}
You have to use NotificationCompat.Builder and then set MediaStyle.
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))
now run and check in android O
If you are using android x
MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(this, "tag");
androidx.media.app.NotificationCompat.MediaStyle mediaStyle = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaStyle = new
androidx.media.app.NotificationCompat.MediaStyle();
mediaStyle.setShowCancelButton(true);
mediaStyle.setShowActionsInCompactView(2,3,4); //index of actions
// shown when notif is collapsed
mediaStyle.setMediaSession(mediaSessionCompat.getSessionToken()); // without this notif won't get colorized
}
Notification notification = notificationCompatBuilder
.setStyle(mediaStyle)
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.drawable.notif_icon)
.setColorized(true)
.setColor(getResources().getColor(R.color.red))
.setLargeIcon(BitmapFactory.decodeResource(
getResources(),
R.drawable.artwork))
.setContentIntent(notifyPendingIntent)
.addAction(R.drawable.ic_music_note_black_24dp,"music note", null)
.addAction(R.drawable.ic_notifications_paused_black_24dp,"pause",
null)
.addAction(R.drawable.ic_skip_previous_black_24dp,"skip", null)
.addAction(R.drawable.ic_pause_black_24dp,"pause", null)
.addAction(R.drawable.ic_skip_next_black_24dp,"next", null)
.setSubText("Song name")
.setDefaults(NotificationCompat.DEFAULT_ALL)
// Set primary color (important for Wear 2.0 Notifications))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.