Android 发送本地通知

本文介绍如何在Android系统API级别26及以上版本中正确发送并兼容显示本地通知的方法,针对旧方法在新版本中可能失效的问题提供解决方案。

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

Android发送本地通知,之前很多方法都收不到通知了,更新完的 兼容api26

				Intent intent = new Intent(mContext,TestActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent,
                        PendingIntent.FLAG_ONE_SHOT);
                final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    int importance = NotificationManager.IMPORTANCE_HIGH;
                    @SuppressLint("WrongConstant")
                    NotificationChannel notificationChannel = new NotificationChannel("ID", "NAME", importance);
                    notificationChannel.enableVibration(true);
                    notificationChannel.setShowBadge(true);
                    notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                    if (notificationManager != null) {
                        notificationManager.createNotificationChannel(notificationChannel);
                    }
                }
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, "ID");
                mBuilder.setSmallIcon(R.mipmap.ic_launcher);
                mBuilder.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                mBuilder.setDefaults(Notification.DEFAULT_ALL);
                mBuilder.setAutoCancel(true);
                mBuilder.setContentTitle("title");
                mBuilder.setContentText("content");
                mBuilder.setContentIntent(pendingIntent);
                mBuilder.setNumber(1);
                final Notification notify = mBuilder.build();

                //随机id 1000-2000
                final int notifyId = (int) (Math.random() * 1000 + 1000);
                if (notificationManager != null) {
                    notificationManager.notify(notifyId, notify);
                }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值