如何知道是否显示通知图标?

时间:2018-05-23 07:26:23

标签: android

我知道我可以使用函数displayNotifyIcon来显示通知图标

  1. 如何知道是否以编程方式显示通知图标?

  2. 如果已经显示通知图标,我调用displayNotifyIcon后发生了什么?

  3. 代码

    fun displayNotifyIcon(mContext: Context, myIntent:Intent, isServer: Boolean=false) {
        myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    
        val pendingIntent =if (isServer) {
            PendingIntent.getService(mContext, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT)
        }  else {
            PendingIntent.getActivity(mContext, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT)
        }
    
        val notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    
    
        var builder = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            val notificationChannel = NotificationChannel(mContext.getString(R.string.NotificationChannelID),
                    mContext.getString(R.string.NotificationChannelName),
                    NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager.createNotificationChannel(notificationChannel)
            NotificationCompat.Builder(mContext, notificationChannel.id)
        } else {
            NotificationCompat.Builder(mContext)
        }
    
        builder.setSmallIcon(R.drawable.smallnotify)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.resources, R.drawable.largernotify))
                .setContentTitle(mContext.getString(R.string.NotificationTitle))
                .setTicker(mContext.getString(R.string.NotificationTitle))
                .setContentText( mContext.getString(R.string.NotificationContent))
                .setContentIntent(pendingIntent)
    
        val notification = builder.build()
        notification.flags = notification.flags or NotificationCompat.FLAG_ONGOING_EVENT 
        notification.flags = notification.flags or NotificationCompat.FLAG_NO_CLEAR 
    
        notificationManager.notify(mContext.resources.getInteger(R.integer.NotificationID), notification)
    }
    
    
    fun clearNotifyIcon(mContext: Context) {
        val notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.cancel(mContext.resources.getInteger(R.integer.NotificationID))
    }
    

1 个答案:

答案 0 :(得分:0)

  

如果已显示通知图标,则在调用displayNotifyIcon后发生了什么

notificationManager.notify(mContext.resources.getInteger(R.integer.NotificationID),notice)

在这里,您使用了一个固定的整数值mContext.resources.getInteger(R.integer.NotificationID),因此它将覆盖现有的通知,并且只会显示最新的通知。