Android——《Android第一行代码》中使用通知 方法,Android8.0系统NotificationCompat.builder()方法的使用及引导用户开启通知

1、该书中使用的Android版本较老,8.0以下,没有渠道这一说法,所以使用的高版本Android系统,需要进行适配,即判断本机系统是否在8.0以上,是的话,添加渠道,方法如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button sendNotice = (Button) findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.send_notice:
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){  //Android O (8.0)以上版本需要渠道
            
                    NotificationChannel notificationChannel = new NotificationChannel("channelid1","channelname",NotificationManager.IMPORTANCE_HIGH);//通知重要度,DEFAULT及以上,通知时手机默认会有振动
                    manager.createNotificationChannel(notificationChannel);
                }
                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"channelid1");
                builder.setContentTitle("This is content title");
                builder.setContentText("This is content text");
                builder.setWhen(System.currentTimeMillis());
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
                manager.notify(1,builder.build());
                break;
                default:
                    break;
        }
    }
}

2、引导用户开启通知

今天再次用了通知加到自己的APP中,结果发现上面好使的程序失效了,经过一番波折,发现是手机系统问题,不知什么时候系统升级优化了,把我的APP   允许通知 关掉了(设置->通知管理->允许通知),因此有必要在程序中加入打开通知引导程序,引导用户打开通知开关:

 //检查手机是否打开通知,若没有,引导用户打开
    public void checkAllowNotify(){
        //判断APP通知是否已经打开
        NotificationManagerCompat notification = NotificationManagerCompat.from(this);
        boolean isEnabled = notification.areNotificationsEnabled();
        //若未打卡,引导用户打开(考虑系统兼容性)
        if (!isEnabled) {
            //未打开通知
            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 intent = new Intent();
                            //android 8.0引导
                            if(Build.VERSION.SDK_INT >=26){
                                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                                intent.putExtra("android.provider.extra.APP_PACKAGE", MyApplication.getContext().getPackageName());
                            }
                            //android 5.0-7.0
                            if(Build.VERSION.SDK_INT >=21 && Build.VERSION.SDK_INT <26) {
                                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                                intent.putExtra("app_package", MyApplication.getContext().getPackageName());
                                intent.putExtra("app_uid", MyApplication.getContext().getApplicationInfo().uid);
                            }
                            //其他
                            if(Build.VERSION.SDK_INT <21){
                                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                intent.setData(Uri.fromParts("package", MyApplication.getContext().getPackageName(), null));}

                            startActivity(intent);

                        }
                    })
                    .create();
            alertDialog.show();
        }

    }

 

你可能感兴趣的:(Android)