Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.azhara.simplenotification
- import android.app.NotificationChannel
- import android.app.NotificationManager
- import android.app.PendingIntent
- import android.content.Context
- import android.content.Intent
- import android.graphics.BitmapFactory
- import android.net.Uri
- import android.os.Build
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.view.View
- import androidx.core.app.NotificationCompat
- class MainActivity : AppCompatActivity() {
- companion object{
- private const val NOTIFICATION_ID = 1
- private const val CHANNEL_ID = "channel_01"
- private const val CHANNEL_NAME = "dicoding channel"
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- }
- //aksi untuk onClick pada buttonv
- fun sendNotification(view: View){
- val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://dicoding.com"))
- val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
- val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- val notifBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
- .setContentIntent(pendingIntent)
- .setSmallIcon(R.drawable.ic_notifications_black_24dp) // Set small icon
- .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_notifications_white_48dp))
- .setContentTitle(resources.getString(R.string.content_title))
- .setContentText(resources.getString(R.string.content_text))
- .setSubText(resources.getString(R.string.subtext))
- .setAutoCancel(true)
- /*
- Untuk android Oreo ke atas perlu menambahkan notification channel
- */
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
- val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
- channel.description = CHANNEL_NAME
- notifBuilder.setChannelId(CHANNEL_ID)
- notificationManager.createNotificationChannel(channel)
- }
- val notification = notifBuilder.build()
- notificationManager.notify(NOTIFICATION_ID, notification)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement