【Android 非常基础】多通知 PendingIntent.getActivity参数使用

本文深入解析了如何使用PendingIntent.getActivity方法的参数实现不同用户消息的独立通知,通过设置requestCode和flags来区分通知来源,确保每个用户的消息以独立的通知形式呈现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在做多通知的时候,一般都希望用户A发过来的消息是用一个通知显示,用户B发过来的消息用另外一个通知显示,互不干扰,那么就得活用PendingIntent.getActivity的参数。

PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)参数进行下分析,重点是第二跟第四个:

requestCode,这个参数就是用来区分不同的通知,例如这个通知是属于A用户的,那么就得传入A的唯一标识。

flags,这是PendingIntent.FLAG_UPDATE_CURRENT即可,更新同一个requestCode的PendingIntent的数据。

最后在发通知的时候,调用NotificationManager.notify(int id, Notification notification),第一个参数也是传入某个用户的唯一标识。

这样才能达到预期效果。






``` private void generateForegroundNotification() { Intent intentMainLanding = new Intent(mContext, MainActivity.class); //set pending intent flag PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intentMainLanding, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); Bitmap iconNotification = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); if (mNotificationManager == null) { mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } NotificationChannelGroup group = new NotificationChannelGroup("chats_group", "Chats"); mNotificationManager.createNotificationChannelGroup(group); NotificationChannel notificationChannel = new NotificationChannel("service_channel", "Service Channel", NotificationManager.IMPORTANCE_DEFAULT); notificationChannel.setDescription("Channel for the foreground service"); mNotificationManager.createNotificationChannel(notificationChannel); NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, "service_channel") .setSmallIcon(R.drawable.number_one) // Set notification icon here .setContentTitle("Foreground Service") .setContentText("MediaPlayback Service is running") .setContentIntent(pendingIntent) .setPriority(NotificationCompat.PRIORITY_DEFAULT); Notification notification = builder.build(); int mNotificationId = 111; startForeground(mNotificationId, notification); }```private void updateNotification(boolean isPlaying) { // 创建带播放/暂停按钮的通知 Intent playIntent = new Intent(this, AudioService.class); playIntent.setAction(isPlaying ? ACTION_PAUSE : ACTION_START); PendingIntent pendingPlayIntent = PendingIntent.getService( this, 0, playIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("背景音乐服务") .setContentText(isPlaying ? "播放中" : "已暂停") .addAction(isPlaying ? R.drawable.ic_pause : R.drawable.ic_play, isPlaying ? "暂停" : "播放", pendingPlayIntent) .addAction(R.drawable.ic_stop, "停止", PendingIntent.getService(this, 0, new Intent(this, AudioService.class).setAction(ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT)); startForeground(NOTIFICATION_ID, builder.build()); } 怎么合在一起?
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值