[Android 앱 개발 숙련] 과제 - 사과마켓 (6)메인 화면 기능 구현3

0
post-thumbnail

🍥구현 기능

  • 메인 화면 기능 요구 사항:
    • 알람 아이콘 클릭 시, Notification 생성하기

🍥구현하기

  • 🔗github link
  • API 30에서 실행한 화면
  • API 34에서 실행한 화면

MainActivity.kt

  • AndroidManifest 파일에 Notification 사용 권한 추가
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        dataSet = getDummyData()
        initRecyclerView(dataSet)

        initAlarmButton()
    }
    
   private fun initAlarmButton() {
        binding.mainIvAlarm.setOnClickListener {
            Log.d(TAG, "alarm button clicked")
            showAlarm()
        }
    }

    private fun showAlarm() {
        val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        val builder: NotificationCompat.Builder

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Log.d(TAG, "build version >= 26")
            
            val channelId = "keyword-channel"
            val channel = NotificationChannel(
                channelId,
                resources.getText(R.string.alarm_channel_name),
                NotificationManager.IMPORTANCE_DEFAULT
            ).apply {
                description = resources.getText(R.string.alarm_channel_description).toString()
                setShowBadge(true)
                val uri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
                val audioAttributes = AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build()
                setSound(uri, audioAttributes)
                enableVibration(true)
            }
            
            manager.createNotificationChannel(channel)
            builder = NotificationCompat.Builder(this, channelId)
        } else {
            Log.d(TAG, "build version < 26")
            
            builder = NotificationCompat.Builder(this)
        }

        val intent = Intent(this, MainActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        val pendingIntent = PendingIntent.getActivity(
            this,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
        
        builder.run {
            setSmallIcon(R.drawable.icon_alarm)
            setWhen(System.currentTimeMillis())
            setContentTitle(resources.getText(R.string.alarm_title))
            setContentText(resources.getText(R.string.alarm_text))
            addAction(R.mipmap.ic_launcher, "Action", pendingIntent)
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            Log.d(TAG, "build version >= 33")
            
            if (!NotificationManagerCompat.from(this).areNotificationsEnabled()) {
                getNotificationPermission()
            }
        }
        Log.d(TAG, "notify alarm")
        manager.notify(11, builder.build())
    }
    
    private fun getNotificationPermission(){
        Log.d(TAG, "get notification permssion")
        val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
            putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
        }
        startActivity(intent)
    }
profile
Be able to be vulnerable, in search of truth

0개의 댓글