Android 알림 표시줄 사용 방법

1. 알림 내용 설정


	//CHANNEL_ID, ID,Android 8.0 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
    		// 
            .setSmallIcon(R.drawable.notification_icon)
            // 
            .setContentTitle(textTitle)
            // 
            .setContentText(textContent)
            // 
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

2. 채널 생성


Android 8.0 이상 버전에서 알림을 제공하려면 시스템에 응용 프로그램의 알림 채널을 등록해야 합니다.

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            // 
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
상기 코드는 응용 프로그램이 시작될 때 즉시 실행되어야 하며 응용 프로그램에 넣어서 초기화할 수 있다.

3. 알림 표시줄의 클릭 조작 설정


일반적으로 알림 표시줄을 클릭하면 다음과 같은 Activity 인터페이스가 열립니다.

	// 
    Intent intent = new Intent(this, AlertDetails.class);
    // , activity , 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    // activity pendingIntent, 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            // pendingIntent
            .setContentIntent(pendingIntent)
            // 
            .setAutoCancel(true);    

4. 알림 표시


    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    //notificationId  , 
    notificationManager.notify(notificationId, builder.build());
직접 보기홈페이지 자습서로 설정할 수 있는 특수 기능도 많다.
이상은 Android 알림 표시줄의 사용 방법에 대한 상세한 내용입니다. Android 알림 표시줄의 사용에 관한 더 많은 자료는 저희 다른 관련 글을 주목해 주십시오!

좋은 웹페이지 즐겨찾기