Advertisement
Smile_Studio

Notification

Jun 27th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.71 KB | None | 0 0
  1. fun sendNotificationOptions(title: String?, message: String?, action: String?, packagename: String?, id: Long?) {
  2.         Debug.e("--- title: $title\n--- message: $message\n--- action: $action\n--- packagename: $packagename\n--- id: $id")
  3.         var intent: Intent? = null
  4.         var pendingIntent: PendingIntent? = null
  5.         if (action == "install" || action == "update" || action == "rate") {
  6.             val install_intent = Intent(Intent.ACTION_VIEW, Uri.parse(AndroidUtils.gotoMaketStore(packagename)))
  7.             pendingIntent = PendingIntent.getActivity(applicationContext, 0, install_intent, PendingIntent.FLAG_ONE_SHOT)
  8.         }
  9.         val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
  10.         val channelID = applicationContext.getString(R.string.default_notification_channel_id)
  11.         val builder = NotificationCompat.Builder(applicationContext, channelID)
  12.         val drawable = ContextCompat.getDrawable(applicationContext, R.mipmap.ic_launcher)
  13.         val bitmap = (drawable as BitmapDrawable).bitmap
  14.         val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  15.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  16.             val CHANNEL_NAME = "CHANNEL_NAME"
  17.             val CHANNEL_DESCRIPTION = "CHANNEL_DESCRIPTION"
  18.             val notificationChannel = NotificationChannel (channelID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
  19.             notificationChannel.setDescription(CHANNEL_DESCRIPTION)
  20.             notificationChannel.enableVibration(true)
  21.             notificationChannel.enableLights(true)
  22.             notificationChannel.canShowBadge()
  23.             notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC)
  24.             notificationChannel.setLightColor(ContextCompat.getColor(applicationContext, R.color.colorAccent))
  25.             notificationManager.createNotificationChannel(notificationChannel)
  26.         }
  27.         val notification = builder.setContentIntent(pendingIntent)
  28.                 .setSmallIcon(R.mipmap.ic_launcher)
  29.                 .setLargeIcon(bitmap)
  30.                 .setTicker(applicationContext.getString(R.string.app_name))
  31.                 .setContentTitle(title)
  32.                 .setStyle(NotificationCompat.BigTextStyle().bigText(message))
  33.                 .setAutoCancel(false)
  34.                 .setSound(defaultSoundUri)
  35.                 .setDefaults(Notification.DEFAULT_ALL)
  36.                 .setContentText(message).build()
  37.         notification.flags = Notification.FLAG_AUTO_CANCEL
  38.         notificationManager.notify(System.currentTimeMillis().toInt(), notification)
  39.         AndroidDeviceInfo.Vibrate(applicationContext, 1000)
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement