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

Android Notification通知

程序员文章站 2023-12-31 23:42:16
先上效果:(注:写错一个字母,就不丢人了!!!)**1、权限**1.1、判断是否拥有权限NotificationManagerCompat.from(this).areNotificationsEnabled();没错只要这一行代码,若果拥有权限则打印日志返回true1.2、用户手动打开权限 Intent localIntent = new Intent(); //直接跳转到应用通知设置的代码:...

先上效果:

Android Notification通知

Android Notification通知

**

1、权限

**
1.1、判断是否拥有权限

NotificationManagerCompat.from(this).areNotificationsEnabled();
	没错只要这一行代码,若果拥有权限则打印日志返回true

1.2、用户手动打开权限
Android Notification通知

 Intent localIntent = new Intent();
                            //直接跳转到应用通知设置的代码:
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0及以上
                                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                            } else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0以上到8.0以下
                                localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                                localIntent.putExtra("app_package", getPackageName());
                                localIntent.putExtra("app_uid", getApplicationInfo().uid);
                            } else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {//4.4
                                localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                localIntent.addCategory(Intent.CATEGORY_DEFAULT);
                                localIntent.setData(Uri.parse("package:" + getPackageName()));
                            } else {
                                //4.4以下没有从app跳转到应用通知设置页面的Action,可考虑跳转到应用详情页面,
                                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                if (Build.VERSION.SDK_INT >= 9) {
                                    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                    localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                                } else if (Build.VERSION.SDK_INT <= 8) {
                                    localIntent.setAction(Intent.ACTION_VIEW);
                                    localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
                                    localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
                                }
                            }
                            startActivity(localIntent);
目前我所知道的只能跳转到这里了,有更好的欢迎指点

1.3、完整代码
Android Notification通知

 if (!NotificationManagerCompat.from(this).areNotificationsEnabled()) {
            //未打开通知
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("请在“通知”中打开通知权限")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    })
                    .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();

                            Intent localIntent = new Intent();
                            //直接跳转到应用通知设置的代码:
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0及以上
                                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                            } else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0以上到8.0以下
                                localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                                localIntent.putExtra("app_package", getPackageName());
                                localIntent.putExtra("app_uid", getApplicationInfo().uid);
                            } else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {//4.4
                                localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                localIntent.addCategory(Intent.CATEGORY_DEFAULT);
                                localIntent.setData(Uri.parse("package:" + getPackageName()));
                            } else {
                                //4.4以下没有从app跳转到应用通知设置页面的Action,可考虑跳转到应用详情页面,
                                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                if (Build.VERSION.SDK_INT >= 9) {
                                    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                    localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                                } else if (Build.VERSION.SDK_INT <= 8) {
                                    localIntent.setAction(Intent.ACTION_VIEW);
                                    localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
                                    localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
                                }
                            }
                            startActivity(localIntent);

                        }
                    })
                    .create();
            alertDialog.show();
            alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.RED);
        }

2、通知与订阅

效果图在最开始,直接上实现代码

2.1、通知

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "chat")
                .setContentTitle("收到一条聊天消息")
                .setContentText("今天中午吃什么?")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
                .setAutoCancel(true)
                .build();
        manager.notify(1, notification);

2.2、订阅

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "subscribe")
                .setContentTitle("收到一条订阅消息")
                .setContentText("地铁沿线30万商铺抢购中!")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
                .setAutoCancel(true)
                .build();
        manager.notify(2, notification);

参考地址:
通知与订阅:https://blog.csdn.net/guolin_blog/article/details/79854070
权限:https://blog.csdn.net/aiynmimi/article/details/102740139?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

本文地址:https://blog.csdn.net/weixin_44259937/article/details/107952559

相关标签: AndroidStudio

上一篇:

下一篇: