我已经成功地显示了通知,但问题是这并不像预期的那样工作。问题是- 1。我不知道如何将通知保持在前2。当我滑动通知时,它会显示snooze图标,但不会在任何其他音乐应用程序中显示。
以下是我的代码
`if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
RemoteViews expandedView = new RemoteViews(getContext().getPackageName(), R.layout.custom_push);
CharSequence name = "Music Player";
String description = "Play music in background";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel("Music Player", name, importance);
channel.setDescription(description);
channel.setShowBadge(false);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationCompat.Builder nc = new NotificationCompat.Builder(getContext(),"Music Player");
Intent notifyIntent = new Intent(getContext(), now_playing.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(),0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setCustomBigContentView(expandedView);
nc.setSmallIcon(R.drawable.notification_icon);
nc.setAutoCancel(false);
nc.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
nc.setOngoing(true);
NotificationManager notificationManager = getActivity().getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
notificationManager.notify(1, nc.build());
}`
此外,我还注意到其他音乐播放器在设置中没有任何通知通道。如果有人能告诉我如何正确实现通知,那就太好了。感谢
发布于 2018-06-09 03:27:07
根据https://developer.android.com/training/notify-user/channels的说法,
1)通知的优先级只能使用NotificationManager .IMPORTANCE_HIGH
设置2)必须知道其他音乐应用程序频道的标识符CHANNEL_ID
,也许这篇文章可以帮助你:https://medium.com/google-developers/migrating-mediastyle-notifications-to-support-android-o-29c7edeca9b7
发布于 2018-06-09 03:52:43
创建通知通道时设置通知重要性高NotificationManger.IMPORTANCE_HIGH
NotificationChannel channel = new NotificationChannel(channelId, ChannelName, NotificationManager.IMPORTANCE_HIGH);
https://stackoverflow.com/questions/50765399
复制相似问题