繁体   English   中英

Android Studio 通知已实现但未显示

[英]Android studio notifications implemented but not showing

我已经在 android studio 中实现了 android 通知。 我正在为媒体播放器创建通知。 以下是显示通知的功能

public void showNotification(int playPauseBtn)
{
Intent intent = new Intent(this, PlayerActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

Intent prevIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PREVIOUS);
PendingIntent prevPending = PendingIntent.getBroadcast(this, 0, prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Intent pauseIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PLAY);
PendingIntent pausePending = PendingIntent.getBroadcast(this, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Intent nextIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_NEXT);
PendingIntent nextPending = PendingIntent.getBroadcast(this, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);

byte[] picture = null;
try
{
    picture = getAlbumArt(listSongs.get(position).getPath());
} catch (Exception ignored)
{
}

Bitmap thumb;
if (picture != null)
{
    thumb = BitmapFactory.decodeByteArray(picture, 0, picture.length);
} else
{
    thumb = BitmapFactory.decodeResource(getResources(), R.drawable.icon_music);
}
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_1).setSmallIcon(playPauseBtn)
        .setLargeIcon(thumb)
        .setContentTitle(listSongs.get(position).getTitle())
        .setContentText(listSongs.get(position).getArtist())
        .addAction(R.drawable.ic_baseline_skip_previous_24, "Previous", prevPending)
        .addAction(R.drawable.ic_baseline_skip_next_24, "Next", nextPending)
        .addAction(playPauseBtn, "pause", pausePending)
        .setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setMediaSession(mediaSessionCompat.getSessionToken()))
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setOnlyAlertOnce(true)
        .setContentIntent(contentIntent)
        .build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
Toast.makeText(PlayerActivity.this, "Nitification created", Toast.LENGTH_SHORT).show();

}

在函数参数( playPauseBtn )中,我根据歌曲正在播放或暂停的天气发送播放图标或暂停图标。

函数调用如下:

showNotification(R.drawable.ic_baseline_pause_24);

要么

showNotification(R.drawable.ic_baseline_play_arrow_24);

但是当我调用这个函数时,通知不会出现。 我也在使用通知渠道,但仍然无法正常工作。 我也尝试调试代码,但代码运行良好,仍然没有显示通知。 请指教

试试这个。

将此代码放在您的活动/片段中;

    public void getNotification(){
    
    int notifId =new Random().nextInt(500);   //get random id
    NotificationManager notificationManager;
    Notification notification;
    notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        channelID = "1";
        channelName = "news";
        channelDesc = "news description";
        NotificationChannel notificationChannel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.setDescription(channelDesc);
        notificationChannel.enableLights(true);
        notificationChannel.setSound(null, null);
        notificationChannel.setLightColor(Color.GREEN);
        notificationManager.createNotificationChannel(notificationChannel);

    }

    RingtoneManager.getRingtone(mContext,
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();

    NotificationCompat.Builder   builder = new NotificationCompat.Builder(mContext, channelID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("title Of MUSIC")
            .setContentText("Content")
            .setVibrate(new long[]{100, 500, 500, 500, 500})
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    Intent actionReadMessage = new Intent(mContext, NotifAction.class);
    actionReadMessage.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    actionReadMessage.setAction("Play");
   actionReadMessage.putExtra("NotifId",notifId);
    PendingIntent playPendingIntent = PendingIntent.getBroadcast(mContext, notifId, actionReadMessage,PendingIntent.FLAG_CANCEL_CURRENT);




    Intent mainAction = new Intent(mContext, NotifAction.class);
    mainAction.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    mainAction.setAction("Pause");
    mainAction.putExtra("NotifId",notifId);
    PendingIntent PausePendingIntent = PendingIntent.getBroadcast(mContext, notifId, mainAction,PendingIntent.FLAG_CANCEL_CURRENT);

    

    builder.setContentIntent(PausePendingIntent);
    builder.addAction(R.mipmap.ic_launcher, "Play", playPendingIntent);
    builder.addAction(R.mipmap.ic_launcher, "Pause", PausePendingIntent);
    
    notification = builder.build();
    notificationManager.notify(notifId,  notification);
    

    }

并创建类 NotifAction.class 来处理所有操作:

public class NotifAction extends BroadcastReceiver {

private static final String TAG = "maybe";
private MediaPlayer mp;
@Override
public void onReceive(final Context context, Intent intent) {

    String action = intent.getAction();
    Bundle extra = intent.getExtras();

    if (extra != null) {
        int notifId = extra.getInt("NotifId");
        if (action.equals("Play")) {
            mp = MediaPlayer.create(context, R.raw.music);
            handleMusicState(mp);
        } else if (action.equals("Pause")) {

            handleMusicState(mp);
            setMessageRead(notifId, context);

        } else {
            Toast.makeText(context, "extra  action !", Toast.LENGTH_SHORT).show();
        }

    } else {
        Toast.makeText(context, "Empty extra !", Toast.LENGTH_SHORT).show();
    }
    }


private void setMessageRead(int id, Context context) {
    //  other  method
    clearNotification(id, context);

   }

private void clearNotification(int id, Context context) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(id);
   }

private void handleMusicState(MediaPlayer mediaPlayer) {
    if (mediaPlayer.isPlaying()) mediaPlayer.pause();
    else mediaPlayer.start();

    }
   }

将此接收器定义为清单:note:in 标签

 <receiver android:name=".NotifAction">
        <intent-filter>
            <action android:name="Play" />
            <action android:name="Pause" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

使固定

.setMediaSession(mediaSessionCompat.getSessionToken())在问题中给出的代码中的.setStyle()中使用.setMediaSession(mediaSessionCompat.getSessionToken())

只需使用

 .setStyle(new androidx.media.app.NotificationCompat.MediaStyle())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM