Android悬浮通知,Notification的设置。

费秦迟
2023-12-01

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

//显示通知栏
    private void showFloat(String message) {
        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);
            
            mManager.createNotificationChannel(mChannel);
           
            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);
        }
    }

 

 类似资料: