消息通知Notificatio在8.0上不显示,适配Android8.0

本文介绍了一个用于安卓设备的通知管理工具类,该工具类采用单例模式,支持从Android 8.0开始的通知渠道创建,并提供了发送通知的方法,包括普通通知和包含额外数据的通知。

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

public class NotificationUtil {

    private Context aContext;
    private NotificationManager notificationManager;

    private static class NotificationHolder {
        private static final NotificationUtil INSTANCE = new NotificationUtil();
    }

    private NotificationUtil() {
    }

    public static final NotificationUtil getInstance() {
        return NotificationHolder.INSTANCE;
    }

    /**
     * 初始化变量和适配8.0工作
     *
     * @param context
     */
    @RequiresApi(api = 26)
    public void init(Context context) {
        aContext = context;
        notificationManager = (NotificationManager) aContext.getSystemService(
                NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            String channelId = "chat";
            String channelName = "聊天消息";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);
        }
    }

    /**
     * 为8.0 设置通知渠道
     *
     * @param channelId
     * @param channelName
     * @param importance
     */
    @RequiresApi(api = 26)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel notificationchannel = new NotificationChannel(channelId, channelName, importance);
        notificationManager.createNotificationChannel(notificationchannel);
    }


    /**
     * 发送通知消息
     *
     * @param title
     * @param content
     */
    public void sendNotification(String title, String content, Context context, Activity activity) {


        Intent intent = new Intent(context, activity.getClass());
        PendingIntent intentPend = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Notification notification = new NotificationCompat.Builder(aContext, "chat")
                .setContentTitle(title)
                .setContentText(content)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(aContext.getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setContentIntent(intentPend)
                .build();
        notificationManager.notify(1, notification);
    }


    /**
     * 发送通知消息
     * bundle
     * @param bundle
     * @param context
     */
    public void sendNotificationBundle(Bundle bundle, Context context, Class activity) {

        Intent intent = new Intent(context, activity);
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent intentPend = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        String content = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        Notification notification = new NotificationCompat.Builder(aContext, "chat")
                .setContentTitle(title)
                .setContentText(content)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(aContext.getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setContentIntent(intentPend)
                .build();
        notificationManager.notify(1, notification);
    }

}

使用方式:NotificationUtil.init(context).sendNotificationBundle();

或者NotificationUtil.init(context).sendNotification();

Activity参数对应的是点击后要跳转的Activity

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄林晴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值