我了解到,其他通知通道现在需要显示通知,以向运行android OS Oreo或更高版本的android设备显示通知,但是我仍然很难理解在通知服务中添加频道的基本知识。
就像在我当前的媒体播放器项目中一样,我想在歌曲开始播放时显示媒体控制通知,因此我按照教程进行操作,能够使通知出现,但仅在运行OS 6.0的设备中(这是我测试过的最低OS)和),而当我尝试在OS 9.0的另一台设备中运行时,什么也没出现。
我认为是因为我的频道设置不正确。
这是处理通知的类
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void handleIntent( Intent intent ) {
if( intent == null || intent.getAction() == null )
return;
String action = intent.getAction();
if( action.equalsIgnoreCase( ACTION_PLAY ) ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mController.getTransportControls().play();
}
} else if( action.equalsIgnoreCase( ACTION_PAUSE ) ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mController.getTransportControls().pause();
}
} else if( action.equalsIgnoreCase( ACTION_FAST_FORWARD ) ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mController.getTransportControls().fastForward();
}
} else if( action.equalsIgnoreCase( ACTION_REWIND ) ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mController.getTransportControls().rewind();
}
} else if( action.equalsIgnoreCase( ACTION_PREVIOUS ) ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mController.getTransportControls().skipToPrevious();
}
} else if( action.equalsIgnoreCase( ACTION_NEXT ) ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mController.getTransportControls().skipToNext();
}
} else if( action.equalsIgnoreCase( ACTION_STOP ) ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mController.getTransportControls().stop();
}
}
}
private Notification.Action generateAction(int icon, String title, String intentAction ) {
Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
intent.setAction( intentAction );
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return new Notification.Action.Builder( icon, title, pendingIntent ).build();
}
return null;
}
private void buildNotification( Notification.Action action ) {
Notification.MediaStyle style = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
style = new Notification.MediaStyle();
}
Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
intent.setAction( ACTION_STOP );
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
Notification.Builder builder = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.custom_launcher)
.setContentTitle("Media Title")
.setContentText("Media Artist")
.setDeleteIntent(pendingIntent)
.setStyle(style);
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
NotificationChannel channel = new NotificationChannel(channelID,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
if (mNotifyMgr != null) {
mNotifyMgr.createNotificationChannel(channel);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
assert builder != null;
builder.addAction( generateAction( android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS ) );
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.addAction( generateAction( android.R.drawable.ic_media_rew, "Rewind", ACTION_REWIND ) );
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.addAction( action );
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.addAction( generateAction( android.R.drawable.ic_media_ff, "Fast Foward", ACTION_FAST_FORWARD ) );
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.addAction( generateAction( android.R.drawable.ic_media_next, "Next", ACTION_NEXT ) );
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
style.setShowActionsInCompactView(0,1,2,3,4);
}
NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
notificationManager.notify( 1, builder.build() );
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if( mManager == null ) {
initMediaSessions();
}
handleIntent( intent );
return super.onStartCommand(intent, flags, startId);
}
private void initMediaSessions() {
mMediaPlayer = new MediaPlayer();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mSession = new MediaSession(getApplicationContext(), "simple player session");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mController =new MediaController(getApplicationContext(), mSession.getSessionToken());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mSession.setCallback(new MediaSession.Callback(){
@Override
public void onPlay() {
super.onPlay();
Log.e( "MediaPlayerService", "onPlay");
buildNotification( generateAction( android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE ) );
}
@Override
public void onPause() {
super.onPause();
Log.e( "MediaPlayerService", "onPause");
buildNotification(generateAction(android.R.drawable.ic_media_play, "Play", ACTION_PLAY));
}
@Override
public void onSkipToNext() {
super.onSkipToNext();
Log.e( "MediaPlayerService", "onSkipToNext");
//Change media here
buildNotification( generateAction( android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE ) );
}
@Override
public void onSkipToPrevious() {
super.onSkipToPrevious();
Log.e( "MediaPlayerService", "onSkipToPrevious");
//Change media here
buildNotification( generateAction( android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE ) );
}
@Override
public void onFastForward() {
super.onFastForward();
Log.e( "MediaPlayerService", "onFastForward");
//Manipulate current media here
}
@Override
public void onRewind() {
super.onRewind();
Log.e( "MediaPlayerService", "onRewind");
//Manipulate current media here
}
@Override
public void onStop() {
super.onStop();
Log.e( "MediaPlayerService", "onStop");
//Stop media player here
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel( 1 );
Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
stopService( intent );
}
@Override
public void onSeekTo(long pos) {
super.onSeekTo(pos);
}
@Override
public void onSetRating(Rating rating) {
super.onSetRating(rating);
}
}
);
}
}
@Override
public boolean onUnbind(Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mSession.release();
}
return super.onUnbind(intent);
}
答案 0 :(得分:1)
要创建NotificationBuilder时,必须将GroupId传递给它。
builder = new Notification.Builder(context, channelId)