알림에서 활동 시작

알림에서 활동을 시작하는 경우 사용자가 예상하는 탐색 환경을 유지해야 합니다. 뒤로를 탭하면 앱의 정상적인 작업 흐름을 통해 홈 화면으로 돌아가고 최근 작업 화면을 열면 활동이 별도의 작업으로 표시되어야 합니다. 탐색 환경을 유지하려면 새로운 작업에서 활동을 시작해야 합니다.

알림의 탭 동작을 설정하는 기본 방법은 알림 만들기에서 설명하지만, 이 페이지에서는 새로운 작업 및 백 스택을 만들기 위해 알림 작업에 PendingIntent를 설정하는 방법에 관해 설명합니다. 하지만, 정확한 설정 방법은 시작하는 활동의 유형에 따라 다릅니다.

일반 활동
앱의 일반 UX 흐름의 일부로 존재하는 활동입니다. 따라서, 사용자가 알림으로부터 활동에 도착하면 사용자가 뒤로를 누르고 앱 계층 구조에서 위로 이동할 수 있도록 새 작업에 완전한 백 스택이 포함되어야 합니다.
특수 활동
사용자는 알림에서 시작할 때만 이 활동을 볼 수 있습니다. 어떤 의미에서 이 활동은 알림 UI를 확장하여 알림 자체에는 표시하기 어려운 정보를 제공합니다. 따라서 이 활동에는 백 스택이 필요하지 않습니다.

일반 활동 PendingIntent 설정

알림에서 '일반 활동'을 시작하려면 다음과 같이 새로운 백 스택을 만들도록 TaskStackBuilder를 사용하여 PendingIntent를 설정합니다.

앱의 활동 계층 구조 정의

앱 매니페스트 파일에서 각 <activity> 요소에 android:parentActivityName 속성을 추가하여 활동의 자연적 계층 구조를 정의합니다. 예:

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!-- MainActivity is the parent for ResultActivity -->
    <activity
        android:name=".ResultActivity"
        android:parentActivityName=".MainActivity" />
        ...
    </activity>
    

백 스택이 있는 PendingIntent 작성

활동의 백 스택을 포함하는 활동을 시작하려면 TaskStackBuilder의 인스턴스를 만들고 addNextIntentWithParentStack()을 호출하여 시작하려는 활동의 Intent를 전달해야 합니다.

위에 설명한 대로 활동의 상위 활동을 정의하기만 하면 getPendingIntent()를 호출하여 전체 백 스택이 포함된 PendingIntent를 수신할 수 있습니다.

Kotlin

    // Create an Intent for the activity you want to start
    val resultIntent = Intent(this, ResultActivity::class.java)
    // Create the TaskStackBuilder
    val resultPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
        // Add the intent, which inflates the back stack
        addNextIntentWithParentStack(resultIntent)
        // Get the PendingIntent containing the entire back stack
        getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
    }
    

자바

    // Create an Intent for the activity you want to start
    Intent resultIntent = new Intent(this, ResultActivity.class);
    // Create the TaskStackBuilder and add the intent, which inflates the back stack
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(resultIntent);
    // Get the PendingIntent containing the entire back stack
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    

필요하다면, TaskStackBuilder.editIntentAt()을 호출하여 스택의 Intent 객체에 인수를 추가할 수 있습니다. 이는 가끔 사용자가 백 스택의 활동까지 위로 이동하는 경우 백 스택의 활동에서 의미 있는 데이터를 표시하도록 하기 위해 필요합니다.

그런 다음, 평소와 같이 PendingIntent를 알림에 전달하면 됩니다.

Kotlin

    val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {
        setContentIntent(resultPendingIntent)
        ...
    }
    with(NotificationManagerCompat.from(this)) {
        notify(NOTIFICATION_ID, builder.build())
    }
    

자바

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
    builder.setContentIntent(resultPendingIntent);
    ...
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
    

특수 활동 PendingIntent 설정

알림에서 시작된 '특수 활동'은 백 스택이 필요하지 않으므로 getActivity()를 호출하여 PendingIntent를 만들 수 있지만, 매니페스트에서 적절한 작업 옵션을 정의했는지도 확인해야 합니다.

  1. 매니페스트의 <activity> 요소에 다음 속성을 추가합니다.
    android:taskAffinity=""
    코드에서 사용할 FLAG_ACTIVITY_NEW_TASK 플래그와 함께 이 속성을 공백으로 설정하면 이 활동이 앱의 기본 작업에 포함되지 않습니다. 앱의 기본 어피니티를 포함하는 기존의 모든 작업은 영향을 받지 않습니다.
    android:excludeFromRecents="true"
    최근 작업에서 새 작업을 제외하므로 사용자는 실수로 새 작업까지 뒤로 이동할 수 없습니다.

    예:

        <activity
            android:name=".ResultActivity"
            android:launchMode="singleTask"
            android:taskAffinity=""
            android:excludeFromRecents="true">
        </activity>
        
  2. 알림을 만들고 내보냅니다.
    1. Activity를 시작하는 Intent를 생성합니다.
    2. FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_CLEAR_TASK 플래그와 함께 setFlags()를 호출하여 새로운 빈 작업에서 시작할 Activity를 설정합니다.
    3. getActivity()를 호출하여 PendingIntent를 만듭니다.

    예:

    Kotlin

        val notifyIntent = Intent(this, ResultActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val notifyPendingIntent = PendingIntent.getActivity(
                this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT
        )
        

    자바

        Intent notifyIntent = new Intent(this, ResultActivity.class);
        // Set the Activity to start in a new, empty task
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        // Create the PendingIntent
        PendingIntent notifyPendingIntent = PendingIntent.getActivity(
                this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT
        );
        
  3. 그런 다음, 평소와 같이 알림에 PendingIntent를 전달하면 됩니다.

    Kotlin

        val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {
            setContentIntent(notifyPendingIntent)
            ...
        }
        with(NotificationManagerCompat.from(this)) {
            notify(NOTIFICATION_ID, builder.build())
        }
        

    자바

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        builder.setContentIntent(notifyPendingIntent);
        ...
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(NOTIFICATION_ID, builder.build());
        

다양한 작업 옵션 및 백 스택 작동 방식에 관한 자세한 내용은 작업 및 백 스택을 참조하세요. 알림을 사용하는 샘플 코드는 Android 알림 샘플을 참조하세요.