欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android悬浮通知,Notification的设置。

程序员文章站 2022-06-21 20:06:45
Android8.0系统的通知需要Channel创建,记录笔记://显示通知栏 private void showFloat(String message) { if (UserInfoManager.getInstance().getMsgActive()) { return; } NotificationManager mManager = (NotificationManager) getSystemService(...

Android8.0系统的通知需要Channel创建,记录笔记:

//显示通知栏
    private void showFloat(String message) {
        if (UserInfoManager.getInstance().getMsgActive()) {
            return;
        }
        NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification;
        Intent intent = new Intent(this, MessageActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel("default", "name", NotificationManager.IMPORTANCE_HIGH);
            mChannel.setShowBadge(true);
            mChannel.enableLights(true);
            //2.0 把通知渠道通过createNotificationChannel()
            mManager.createNotificationChannel(mChannel);
            //3.0 Notification这时候可以正常工作了
            notification = new NotificationCompat.Builder(this, "default")
                    .setContentTitle("标题")//设置通知栏标题
                    .setContentText(message) //设置通知栏显示内容
                    .setWhen(System.currentTimeMillis())//通知产生的时间。
                    .setSmallIcon(R.mipmap.app_icon)//设置通知小ICON
                    .setDefaults(NotificationManager.IMPORTANCE_HIGH)
                    .setPriority(NotificationManager.IMPORTANCE_HIGH)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    //.setFullScreenIntent(pendingIntent, true)
                    .setContentIntent(pendingIntent)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon))//设置通知大ICON
                    .build();
        } else {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "default")
                    .setContentTitle("标题")
                    .setContentText(message)
                    .setWhen(System.currentTimeMillis())//通知产生的时间。
                    .setSmallIcon(R.mipmap.app_icon)
                    .setDefaults(NotificationCompat.PRIORITY_HIGH)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setDefaults(NotificationCompat.DEFAULT_SOUND)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    //.setFullScreenIntent(pendingIntent, true)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon));
            notification = notificationBuilder.build();
        }
        mManager.notify(1, notification);
    }
.setAutoCancel(true)表示点击通知栏就自动消失,

如果在退出App的时候需要取消掉通知,可以调用

if (mManager != null) {
    //取消通知
    mManager.cancelAll();
}

下面是检查是否有通知权限,和进入设置的方法:

//是否有通知权限
    public static boolean isOpenNotify(Context context) {
        boolean isOpened = false;
        try {
            NotificationManagerCompat from = NotificationManagerCompat.from(context);
            isOpened = from.areNotificationsEnabled();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isOpened;
    }

//前往设置
    public static void goToSetNotify(Context context) {
        if(Build.VERSION.SDK_INT >=26) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE,context.getPackageName());
            context.startActivity(intent);
        } else {
            Intent intent = new Intent();
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package",context.getApplicationContext().getPackageName());
            intent.putExtra("app_uid",context.getApplicationInfo().uid);
            context.startActivity(intent);
        }
    }

 

本文地址:https://blog.csdn.net/qq137464739/article/details/107360272

相关标签: android