自定义通知单击侦听器无法在上面的android api 26中工作

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (68)

通知工作完全低于api lavel 26但26及以上的android版本通知显示但是点击事件不起作用。我有MusicPlayerReceiver来处理点击。26以上的api MusicPlayerReceiver没有通话。但是吼叫26完全称呼。我检查了stackoverflow解决方案,但这对我来说都不起作用。

 private void createNotification(SuraDetail mSongDetail) {
                try {
                    String songName = mSongDetail.getTitle();
                    String authorName = mSongDetail.getArtist();
                    String albumName = mSongDetail.getDisplay_name();
                    SuraDetail audioInfo = MediaController.getInstance().getPlayingSongDetail();

                    RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.player_small_notification);
                    RemoteViews expandedView = null;
                    if (supportBigNotifications) {
                        expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.player_small_notification);
                    }

                    Intent intent = new Intent(MyApplication.applicationContext, PobitroQuranDetailsActivity.class);
                    intent.setAction("openplayer");
                    intent.setFlags(32768);
                    PendingIntent contentIntent = PendingIntent.getActivity(MyApplication.applicationContext, 0, intent, 0);

                    Notification notification = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.drawable.quran)
                            .setContentIntent(contentIntent).setContentTitle(songName).build();

                    notification.contentView = simpleContentView;
                    if (supportBigNotifications) {
                        notification.bigContentView = expandedView;
                    }

                    setListeners(simpleContentView);
                    if (supportBigNotifications) {
                        setListeners(expandedView);
                    }

                    Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover(MyApplication.applicationContext) : null;

                    if (albumArt != null) {
                        notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
                        if (supportBigNotifications) {
                            notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
                        }
                    } else {
                        notification.contentView.setImageViewResource(R.id.player_album_art, R.drawable.quran);
                        if (supportBigNotifications) {
                            notification.bigContentView.setImageViewResource(R.id.player_album_art, R.drawable.quran);
                        }
                    }
                    notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
                    notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
                    notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
                    if (supportBigNotifications) {
                        notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
                        notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
                        notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
                    }

                    if (MediaController.getInstance().isAudioPaused()) {
                        notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
                        notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
                        if (supportBigNotifications) {
                            notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
                            notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
                        }
                    } else {
                        notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                        notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
                        if (supportBigNotifications) {
                            notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                            notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
                        }
                    }

                    notification.contentView.setTextViewText(R.id.player_song_name, songName);
                    notification.contentView.setTextViewText(R.id.player_author_name, authorName);
                    if (supportBigNotifications) {
                        notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
                        notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
        //                notification.bigContentView.setTextViewText(R.id.player_albumname, albumName);
                    }
                    notification.flags |= Notification.FLAG_ONGOING_EVENT;
                    startForeground(1, notification);

                    if (remoteControlClient != null) {
                        RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
                        metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
                        metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
                   /*     if (audioInfo != null && audioInfo.getCover(MyApplication.applicationContext) != null) {
                            metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                                    audioInfo.getCover(MyApplication.applicationContext));
                        }*/
                        metadataEditor.apply();
                        audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            public void setListeners(RemoteViews view) {
                try {
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PREVIOUS),
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    view.setOnClickPendingIntent(R.id.player_previous, pendingIntent);
                    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_CLOSE), PendingIntent.FLAG_UPDATE_CURRENT);
                    view.setOnClickPendingIntent(R.id.player_close, pendingIntent);
                    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
                    view.setOnClickPendingIntent(R.id.player_pause, pendingIntent);
                    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_NEXT), PendingIntent.FLAG_UPDATE_CURRENT);
                    view.setOnClickPendingIntent(R.id.player_next, pendingIntent);
                    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PLAY), PendingIntent.FLAG_UPDATE_CURRENT);
                    view.setOnClickPendingIntent(R.id.player_play, pendingIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
提问于
用户回答回答于

我在我的应用中发现了完全相同的问题。问题显然是由构建意图的方式引起的,因此不是调用

new Intent(String action);

它现在需要调用

new Intent (String action, 
            Uri uri, 
            Context packageContext, 
            Class<?> cls)

热门问答

使用文档遇到问题,可以找谁处理?

请叫我雷锋!腾讯云文档有“文档反馈”的功能,直接在文档页面就可以反馈,反馈还有 礼品 ~ 云+社区:问答.png ... 展开详请

直播间IM消息可以下载到自己服务器备份吗?

人生的旅途辣鸡前端
推荐
目前仅私有群(Private)、公开群(Public)和聊天室(ChatRoom)具备消息漫游能力(默认7天)。核实下您那边的群组类型看是否支持https://cloud.tencent.com/document/product/269/1502#.E6.B6.88.E6.81....... 展开详请

腾讯云容器加速地址是错的?

推荐已采纳

我刚才测试了一下,这个加速器地址可以用额,你这边是怎么测试出是关闭状态的啊?

加速器地址是不能直接用浏览器访问的,阿里云提供的直接访问也是空

webIM添加组员报错?

无聊至极互联网重度用户
推荐已采纳

付费超过组数限制,核实下是否超过限制

https://cloud.tencent.com/document/product/269/11673#.E5.8A.9F.E8.83.BD.E5.AF.B9.E6.AF.94

Tencent iot-sdk-embedded-c在Windows下编译出错:无法解析外部符号?

无聊至极互联网重度用户
推荐已采纳

腾讯云IM调用 add_group_member提示该群不能邀请成员?

推荐已采纳
是什么类型的群?根据相应类型的邀请他人入群的控制项,对照文档看下是否允许邀请他人入群。 参考文档:https://cloud.tencent.com/document/product/269/1502#.E7.BE.A4.E6.88.90.E5.91.98.E6.93.8D.E4...... 展开详请

所属标签

扫码关注云+社区

领取腾讯云代金券