Create Custom Big Notifications

前端 未结 2 515
梦毁少年i
梦毁少年i 2020-11-29 00:51

I wanted to create a Notification including some controls. Since text and controls are small with default notification size (64dp), i wanted have it larger than default size

2条回答
  •  一生所求
    2020-11-29 01:12

    Using Kotlin make custom notification
    dialog_custom_notification

                        
    
                            
    
                                
                            
    
                            
    
                                
    
                                
    
                            
    
                            
    
                                
                            
                        
                    
    
            shape_bg_main_notification
            
            
                
    
                
    
            
    
    
    
            @SuppressLint("WrongConstant")
                        fun showOfflineNotification(context: Context, title: String, description: String) {
                            val NOTIFICATION_CHANNEL_ID = "com.myapp"
                            val intent = Intent(context, HomeActivity::class.java)
                            intent.putExtra("notification", 1)
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                            if (intent != null) {
                                val pendingIntent = PendingIntent.getActivity(
                                    context, getTwoDigitRandomNo(), intent,
                                    PendingIntent.FLAG_ONE_SHOT
                                )
                                val defaultSoundUri =
                                    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    
                                val remoteCollapsedViews = RemoteViews(packageName, R.layout.dialog_custom_notification)
                                remoteCollapsedViews.setTextViewText(R.id.tvNotificationTitle, title)
                                remoteCollapsedViews.setTextViewText(R.id.tvNotificationDescription, description)
                                remoteCollapsedViews.setTextViewText(R.id.tvDateTime, getTime())
    
                                val notificationBuilder = NotificationCompat.Builder(context)
                                notificationBuilder.setCustomBigContentView(remoteCollapsedViews)
                                notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_round)
                                notificationBuilder.setLargeIcon(
                                    BitmapFactory.de
    
            codeResource(
                                            context.resources,
                                            R.mipmap.ic_launcher
                                        )
                                    )
                                    notificationBuilder.setBadgeIconType(R.mipmap.ic_launcher_round)
                                    notificationBuilder.setContentTitle(title)
                                    if (description != null) {
                                        notificationBuilder.setContentText(description)
                                        notificationBuilder.setStyle(
                                            NotificationCompat.BigTextStyle().bigText(description)
                                        )
                                    }
                                    notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH)
                                    notificationBuilder.setAutoCancel(true)
                                    notificationBuilder.setSound(defaultSoundUri)
                                    notificationBuilder.setVibrate(longArrayOf(1000, 1000))
                                    notificationBuilder.setContentIntent(pendingIntent)
    
                                    val notificationManager =
                                        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                        val importance = NotificationManager.IMPORTANCE_MAX
                                        val notificationChannel = NotificationChannel(
                                            NOTIFICATION_CHANNEL_ID,
                                            "NOTIFICATION_CHANNEL_NAME",
                                            importance
                                        )
                                        notificationChannel.enableLights(true)
                                        notificationChannel.lightColor = Color.RED
                                        notificationChannel.enableVibration(true)
                                        notificationChannel.vibrationPattern = longArrayOf(1000, 1000)
                                        assert(notificationManager != null)
                                        notificationBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
                                        notificationManager.createNotificationChannel(notificationChannel)
                                    }
                                    notificationManager.notify(
                                        getTwoDigitRandomNo()/*Id of Notification*/,
                                        notificationBuilder.build()
                                    )
                                }
                            }
    
                            private fun getTime(): String {
                                val calendar = Calendar.getInstance()
                                val mdformat = SimpleDateFormat("HH:mm")
                                val strDate = mdformat.format(calendar.time)
                                return strDate
                            }
    
                            fun getTwoDigitRandomNo(): Int {
                                return Random().nextInt(90) + 10
                            }
    
    
    
          [1]: https://me.stack.imgur.com/rQFP8.png
    
    
      [1]: https://me.stack.imgur.com/fKM9C.png
    

提交回复
热议问题