【问题标题】:setSmallIcon() and setLargeIcon() not working in Android NotificationsetSmallIcon() 和 setLargeIcon() 在 Android 通知中不起作用
【发布时间】:2019-05-30 11:25:39
【问题描述】:

我正在从 Firebase 控制台推送通知。我能够轻松接收通知数据并且通知也会出现,但我要做的是更改通知的小图标和大图标。

我正在使用这两种方法,但它们似乎都不起作用。我还尝试通过res>New>Vector>Clip Art 使用Vector 选项制作小图标。 既不出现小图标,也不出现大图标,通知也无法展开。

MessagingService.kt

class MessagingService(): FirebaseMessagingService() {

  override fun onMessageReceived(p0: RemoteMessage ? ) {
    super.onMessageReceived(p0)
    showNotification(p0!!.notification!!.title!!, p0!!.notification!!.body!!)
  }

  fun showNotification(title: String, body: String) {
    val icon = BitmapFactory.decodeResource(resources,
      R.drawable.iphn)
    NotificationCompat.Builder(this, "MyNotifications")
      .setLargeIcon(icon)
      .setSmallIcon(R.drawable.ic_notif)
      .setContentTitle(title)
      .setContentText(body)
      .setStyle(NotificationCompat.BigPictureStyle()
        .bigPicture(icon)
        .bigLargeIcon(null))
      .build()

  }
}

ic_notif是我使用Vector创建的drawable

【问题讨论】:

    标签: java android kotlin push-notification android-notifications


    【解决方案1】:

    您可以尝试使用 ic_notif.png 代替矢量。

    除此之外,在最新的Android版本中,建议使用channelId。你可以添加这个块来添加channelId

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channelId = "yourChannelId"
            val channel = NotificationChannel(channelId, "your channel Name" ,
                    NotificationManager.IMPORTANCE_DEFAULT)
            mNotificationManager.createNotificationChannel(channel)
            mBuilder.setChannelId(channelId)
        }
    

    【讨论】:

      【解决方案2】:
      private var mBuilder: NotificationCompat.Builder? = null  
      val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
      val intent = Intent(activity, MainActivity::class.java)
      val pi = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
      
      
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {            
           val icon = BitmapFactory.decodeResource(resources, shapemyapp.admin.R.drawable.iphn)                                    
           val importance = NotificationManager.IMPORTANCE_DEFAULT
           val notificationChannel = NotificationChannel("ID", "Name", importance)
           mNotificationManager.createNotificationChannel(notificationChannel)
           mBuilder = NotificationCompat.Builder(this, "MyNotifications")
                .setLargeIcon(icon)
                .setSmallIcon(R.drawable.ic_notifications_active_black_24dp)
                .setContentTitle(title)
                .setContentText(body)              
                .build()
        } else {
              mBuilder = NotificationCompat.Builder(activity) 
                 .setSmallIcon(R.drawable.ic_notifications_active_black_24dp)
        }                                         
          mBuilder!!.setContentIntent(pi)
          mNotificationManager.notify(NotificationID.id, mBuilder!!.build())
      

      【讨论】:

      • 我在Pie 中运行此代码。它不应该工作吗?
      【解决方案3】:

      这是工作示例:

      onMessageReceived(remoteMessage: RemoteMessage?) 内调用sendNotification(yourResponse),这样您可以静态或动态设置大图标。

      private fun sendNotification(response: NotifyResponse) {
              var bmp: Bitmap? = null
              try {
      //loading the image from server 
      
      
      //            if (!TextUtils.isEmpty(response.image)) {
      //                val futureTarget = GlideApp.with(this).asBitmap().load(response.image).submit()
      //                try {
      //                    bmp = futureTarget.get()
      //                    GlideApp.with(this).clear(futureTarget)
      //
      //                } catch (e: ExecutionException) {
      //                    e.printStackTrace()
      //                } catch (e: InterruptedException) {
      //                    e.printStackTrace()
      //                }
      //
      //            }
      
                  val uniqueInt = (System.currentTimeMillis() and 0xfffffff).toInt()
                  val pendingIntent = PendingIntent.getActivity(this, uniqueInt, getIntent(response.type), PendingIntent.FLAG_ONE_SHOT)
      
                  val channelId = BuildConfig.FLAVOR
                  val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
                  val notificationBuilder = NotificationCompat.Builder(this, channelId)
                          .setSmallIcon(notificationIcon)
                          .setContentTitle(response.title)
                          .setContentText(response.message)
                          .setAutoCancel(true)
                          .setSound(soundUri)
                          .setPriority(NotificationCompat.PRIORITY_HIGH)
                  if (bmp != null) {
                      notificationBuilder.setLargeIcon(bmp)
                      notificationBuilder.setStyle(NotificationCompat.BigPictureStyle().bigPicture(bmp).bigLargeIcon(null))
                  }
                  notificationBuilder.setContentIntent(pendingIntent)
      
      
                  val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
      
      
                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                      val channel = NotificationChannel(BuildConfig.FLAVOR,
                              "Channel human readable title",
                              NotificationManager.IMPORTANCE_DEFAULT)
                      notificationManager.createNotificationChannel(channel)
                  }
      
                  notificationManager.notify(0, notificationBuilder.build())
              } catch (o: Exception) {
                  o.printStackTrace()
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多