首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在API 26中添加音乐播放器通知

如何在API 26中添加音乐播放器通知
EN

Stack Overflow用户
提问于 2018-06-09 01:03:33
回答 2查看 2.3K关注 0票数 2

我已经成功地显示了通知,但问题是这并不像预期的那样工作。问题是- 1。我不知道如何将通知保持在前2。当我滑动通知时,它会显示snooze图标,但不会在任何其他音乐应用程序中显示。

以下是我的代码

代码语言:javascript
复制
`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());
                }`

此外,我还注意到其他音乐播放器在设置中没有任何通知通道。如果有人能告诉我如何正确实现通知,那就太好了。感谢

EN

回答 2

Stack Overflow用户

发布于 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

票数 0
EN

Stack Overflow用户

发布于 2018-06-09 03:52:43

创建通知通道时设置通知重要性高NotificationManger.IMPORTANCE_HIGH

代码语言:javascript
复制
  NotificationChannel channel = new NotificationChannel(channelId, ChannelName, NotificationManager.IMPORTANCE_HIGH);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50765399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档