안드로이드

[안드로이드] 앱 알림설정 on off 활성 비활성화 차단 방법

IT꿈나무 2022. 4. 26. 15:49
반응형

앱 알림 설정이 on/off 확인 코드

NotificationManagerCompat.from(context).areNotificationsEnabled();

앱 알림 설정 bordcastReceiver

<receiver
    android:name=".BlockStateChangedReceiver"
    android:enabled="true"
    android:exported="true"
    >
    <intent-filter>
        <action android:name="android.app.action.APP_BLOCK_STATE_CHANGED" />
        <action android:name="android.app.action.NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED" />
        <action android:name="android.app.action.NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED" />
    </intent-filter>
</receiver>
class BlockStateChangedReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val blockedState = intent.getBooleanExtra(NotificationManager.EXTRA_BLOCKED_STATE, false)
        when (intent.action) {
            NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED -> {
                // すべての通知の有効無効が変化した
                // 앱 알림 설정 on/off 변경시 수신됨.
            }
            NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED -> {
                val groupId =  intent.getStringExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID)
                // groupIdのチャンネルグループの有効無効が変化した
            }
            NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED -> {
                val channelId =  intent.getStringExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID)
                // channleIdのチャンネルの有効無効が変化した
            }
        }
    }
}

참고자료:

https://intrepidgeeks.com/tutorial/how-to-change-the-valid-or-invalid-status-of-notifications-received-through-the-application

반응형