首页 > 解决方案 > 如何使用支持库 v25 显示音乐播放器通知

问题描述

我使用以下代码显示自定义通知,但未显示。以下内容来自我的服务类。单击歌曲后播放成功,除通知外一切正常。

编译 sdk 版本 25 - 最小 sdk 21 - 目标 sdk 25

private void updateNotification ( ) {

     //   try {

            // normal notif
            RemoteViews smallView = new RemoteViews ( getPackageName ( ), R.layout.notification );
            smallView.setTextViewText ( R.id.notification_song_name, currentSong.getTitle ( ) );
            smallView.setTextViewText ( R.id.notification_artist_name, currentSong.getArtist ( ) );
            // expanded notif
            RemoteViews expanedView = new RemoteViews ( getPackageName ( ), R.layout.notification_expanded );
            expanedView.setTextViewText ( R.id.notification_song_name, currentSong.getTitle ( ) );
            expanedView.setTextViewText ( R.id.notification_album_name, currentSong.getAlbum ( ) );
            expanedView.setTextViewText ( R.id.notification_artist_name, currentSong.getArtist ( ) );



            if ( PlayerConstants.SONG_PAUSED ) {
                smallView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_play_arrow_black_24dp ) ).getBitmap ( ) );
                expanedView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_play_arrow_black_24dp ) ).getBitmap ( ) );
            }

            else {
                smallView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_pause_black_24dp ) ).getBitmap ( ) );
                expanedView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_pause_black_24dp ) ).getBitmap ( ) );
            }

            if ( currentSong.getIsLiked ( this ) )
                expanedView.setImageViewBitmap ( R.id.notification_favorite_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.drawable.ic_favorite_red_24dp ) ).getBitmap ( ) );

            else
                expanedView.setImageViewBitmap ( R.id.notification_favorite_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_favorite_border_black_24dp ) ).getBitmap ( ) );


            setListeners ( smallView );
            setListeners ( expanedView );


            Bitmap albumArt = AudioExtensionMethods.getBitMap ( getBaseContext ( ), currentSong.getAlbumArtLocation ( ) );
            if ( albumArt != null ) {
                smallView.setImageViewBitmap ( R.id.notification_album_art, albumArt );
                expanedView.setImageViewBitmap ( R.id.notification_album_art, albumArt );
            }

            else {
                smallView.setImageViewBitmap ( R.id.notification_album_art, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.unkown_album_art ) ).getBitmap ( ) );
                expanedView.setImageViewBitmap ( R.id.notification_album_art, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.unkown_album_art ) ).getBitmap ( ) );
            }



            // create an ongoing notif
            NotificationCompat.Builder builder = new NotificationCompat.Builder ( this )
                .setContent ( smallView )
                .setSmallIcon ( R.mipmap.launcher_icon )
                .setOngoing ( true );

            // makes the notif dismissable when playback is paused
            if ( PlayerConstants.SONG_PAUSED ) {
                builder.setOngoing ( false );
            }



            if ( currentVersionSupportBigNotification ) {
                builder.setCustomBigContentView ( expanedView );
            }

            Intent nIntent = new Intent ( this, MainActivity.class );
            nIntent.putExtra ( "notificationIntent", true );
            nIntent.addFlags ( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
            PendingIntent pendingIntent = PendingIntent.getActivity ( this, 0, nIntent, PendingIntent.FLAG_UPDATE_CURRENT );
            builder.setContentIntent ( pendingIntent );

            // send the notification
            Notification build = builder.build ( );
            notificationManager = (NotificationManager) getSystemService ( NOTIFICATION_SERVICE );
            notificationManager.notify ( NOTIFICATION_ID, build );
     //   }

      //  catch (Exception ignored) {}
    }

我不知道这里出了什么问题。任何帮助,将不胜感激。PS 我不愿意更新我的库,因为更新会使应用程序崩溃,而且我不能从头开始编写整个代码。

标签: androidandroid-notifications

解决方案


哇。有趣的是我是如何解决的。事实证明,通知以及小部件不支持主题或您所称的任何内容。我在通知的布局中使用了“attr/primaryColor”,将这些更改为正常的颜色格式解决了这个问题。


推荐阅读