Advertisement
Guest User

SimpleNotification

a guest
Jan 26th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. package com.azhara.simplenotification
  2.  
  3. import android.app.NotificationChannel
  4. import android.app.NotificationManager
  5. import android.app.PendingIntent
  6. import android.content.Context
  7. import android.content.Intent
  8. import android.graphics.BitmapFactory
  9. import android.net.Uri
  10. import android.os.Build
  11. import androidx.appcompat.app.AppCompatActivity
  12. import android.os.Bundle
  13. import android.view.View
  14. import androidx.core.app.NotificationCompat
  15.  
  16. class MainActivity : AppCompatActivity() {
  17.  
  18. companion object{
  19. private const val NOTIFICATION_ID = 1
  20. private const val CHANNEL_ID = "channel_01"
  21. private const val CHANNEL_NAME = "dicoding channel"
  22. }
  23.  
  24. override fun onCreate(savedInstanceState: Bundle?) {
  25. super.onCreate(savedInstanceState)
  26. setContentView(R.layout.activity_main)
  27. }
  28.  
  29. //aksi untuk onClick pada buttonv
  30. fun sendNotification(view: View){
  31.  
  32. val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://dicoding.com"))
  33. val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
  34.  
  35. val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  36.  
  37. val notifBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
  38. .setContentIntent(pendingIntent)
  39. .setSmallIcon(R.drawable.ic_notifications_black_24dp) // Set small icon
  40. .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_notifications_white_48dp))
  41. .setContentTitle(resources.getString(R.string.content_title))
  42. .setContentText(resources.getString(R.string.content_text))
  43. .setSubText(resources.getString(R.string.subtext))
  44. .setAutoCancel(true)
  45.  
  46. /*
  47. Untuk android Oreo ke atas perlu menambahkan notification channel
  48. */
  49. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
  50.  
  51. val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
  52.  
  53. channel.description = CHANNEL_NAME
  54.  
  55. notifBuilder.setChannelId(CHANNEL_ID)
  56.  
  57. notificationManager.createNotificationChannel(channel)
  58.  
  59. }
  60.  
  61. val notification = notifBuilder.build()
  62.  
  63. notificationManager.notify(NOTIFICATION_ID, notification)
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement