Android Notification的使用

在AndroidManifest.xml中添加权限

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

常用通知的写法,兼容android 8.0之后的版本

    private NotificationManager getNotificationManager() {
        return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    private Notification getNotification(String title) {
        //这里点击通知跳转的以MainActivity为例
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        CharSequence name = "test";
        String description = "test_description";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("test1", name, importance);
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{0, 1000, 1000, 1000});
        NotificationManager manager = getNotificationManager();
        NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
                .setContentTitle(title)
                .setContentText("this is content text 123456789")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setChannelId("test1")
                .setContentIntent(pi)
                .setLights(Color.GREEN, 1000, 1000)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setAutoCancel(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//26,android 8.0
            channel.setDescription(description);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }

        //跳转通知管理页
        boolean isNotifyEnable = NotificationManagerCompat.from(getApplicationContext()).areNotificationsEnabled();
        boolean isChannelEnable = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//26,android 8.0
            isChannelEnable = channel.getImportance() != NotificationManager.IMPORTANCE_NONE;
        }
        if (isNotifyEnable && isChannelEnable) {//发送通知,或者做一些操作,比如设置通知内容
            //manager.notify(1, notification);
            notification.setContentText("123456789");
        } else if (!isNotifyEnable) {
            Intent intent = new Intent();
            if (!(getApplicationContext() instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//26,android 8.0
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                getApplicationContext().startActivity(intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName()));
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//21,android 5.0
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                getApplicationContext().startActivity(intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName()));
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {//9,android 2.3
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setData(Uri.fromParts("package", getPackageName(), null));
                getApplicationContext().startActivity(intent);
            } else {
                System.out.println("低于9没有适配必要");
            }
        } else {
            System.out.println("只打开了通知开关,但是关闭了当前channel的通知,开发者需要根据通知重要性,自行决定如何提示用户");
        }
        return notification.build();
    }
    
    //发送通知
    public void sendNotification(String title) {
        getNotificationManager().notify(1,getNotification(title));
    }

你可能感兴趣的:(Android,android)