Android——通知欄Notification

Android在不同版本會對底層進行一些修改,有趣的是一般大的修改都是在雙數,例如Android6.0是一個分水嶺,基本上一些重要的權限都要在代碼中進行動態的適配,例如下面一段代碼

//Android 6.0以上請求權限
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
				// Android M Permission check
				if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
					requestPermissions(new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_COARSE_LOCATION);
				}else {
					intent.setClass(this, MipcaActivityCapture.class);
					intent.putExtra(Constants.PAGETAG, Constants.BINDTYPEBOX);
					startActivity(intent);
					finish();
				}
			}else {
				intent.setClass(this, MipcaActivityCapture.class);
				intent.putExtra(Constants.PAGETAG, Constants.BINDTYPEBOX);
				startActivity(intent);
				finish();
			}

android8.0也是一個比較大的分支,基本上8.0以上的生產環境都要進行Service適配,例如:

 @SuppressLint("NewApi")
    void init() {
        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            this.startForegroundService(new Intent(this, BleService.class));
        }else{
            this.startService(new Intent(this, BleService.class));
        }
    }

好了,言歸正傳!在Android9.0以下,設置通知欄的方法很簡單,代碼就不展示了,但是隨着SDK版本更新,會發現在Android9.0系統中通知經常會收不到,到設置裏面去看,也沒有關閉允許通知,查閱文檔時發現谷歌在9.0系統中,對通知進行了嚴格的限制!

怎麼設置?


public class MainActivity extends AppCompatActivity {


    Button btn_music, btn_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_music = findViewById(R.id.btn_music);
        btn_message = findViewById(R.id.btn_message);

        btn_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendChatMsg();
            }
        });

        btn_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendSubscribeMsg();
            }
        });




        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "message";
            String channelName = "普通消息";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);

            channelId = "payment";
            channelName = "支付消息";
            importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotificationChannel(channelId, channelName, importance);
        }
        Observable();

    }


    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }


    public void sendChatMsg() {
        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);
    }

    public void sendSubscribeMsg() {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "subscribe")
                .setContentTitle("收到轉賬!")
                .setContentText("支付寶到賬500萬元!")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .build();
        manager.notify(2, notification);
    }
}

步驟:1.首先,需要對手機版本進行適配,9.0以下通知方式不變,9.0以上才需要適配

           2.需要註冊一個ChannelID,用來給消息分類型,比如說重要的消息、支付消息、聊天消息、普通消息、廣告等

           3.採用manager.notift(channelID , Notification)的方式進行發送;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章