【问题标题】:How to set a bigger icon in Firebase app notification?如何在 Firebase 应用通知中设置更大的图标?
【发布时间】:2017-05-19 16:11:01
【问题描述】:

您好,我正在使用 Firebase 在 Android 中实现推送通知。现在,一个圆圈内显示了一个小图标。我需要它以更大的尺寸显示。请看图片。这是我的代码,

            android:id="@+id/relativeNotification"

            android:layout_width="192dp"
            android:layout_height="192dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true">

            <com.inspius.coreapp.widget.TintableImageView
                android:layout_width="192dp"
                android:layout_height="192dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_gravity="center_vertical"
                android:contentDescription="@string/app_name"
                android:src="@drawable/ic_launcher"
                app:tint="@color/custom_icon_video_detail_selector" />

如何从这个小图标设置大图标?

【问题讨论】:

  • 您可以添加代码以显示通知吗?

标签: android firebase android-notifications firebase-cloud-messaging


【解决方案1】:

我认为可以通过覆盖默认设置来完成。

在您的应用程序清单中:

<meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/notification_icon" />

使用您的图标代替@drawable/notification_icon

Source

希望有帮助

更新:另外,看看:

https://github.com/firebase/quickstart-android/issues/4#issuecomment-221344318

icon 参数可用于在您的应用程序中指定可绘制对象。 如果你想使用 R.drawable.foo,只需传递 foo。

这里是icon参数文档:

https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support

【讨论】:

    【解决方案2】:

    请参阅只有当您的应用处于前台时,您才能自定义 Firebase 通知。

    您的通知将被 FirebaseMessagingService 服务的 onMessageReceived 方法捕获

    在您的应用处于前台时自定义通知。

    从应用程序可绘制文件夹中放置图标

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icons);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setLargeIcon(largeIcon)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle(remoteMessage.getNotification().getTitle())
    .setContentText(remoteMessage.getNotification().getBody());
    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                manager.notify(0, builder.build());

    从 API 的“icon”标签中放置图标

    String name = remoteMessage.getNotification().getIcon();
    URL url_value = new URL(name);
    Bitmap mIcon1 = BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setLargeIcon(mIcon1)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle(remoteMessage.getNotification().getTitle())            .setContentText(remoteMessage.getNotification().getBody());
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                manager.notify(0, builder.build());

    【讨论】:

      【解决方案3】:

      我尝试做同样的事情。覆盖“handleIntent”方法对我有用。我猜想删除 super.handleIntent(intent) 可以完成这项工作。然后我们可以构建自己的通知来设置大图标。前台和后台案例都会调用此方法。像这样的:

      public class YourMessagingService extends FirebaseMessagingService {
      
          @Override
          public void handleIntent(Intent intent) {
              // super.handleIntent(intent);
      
              // get intent codes....
      
              NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
              Notification notification = builder
                      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.yourLargeIcon))
                      .setSmallIcon(R.drawable.yourSmallIcon)
                      .setContentTitle("yourTitle")
                      .setContentText("yourText")
                      .build();
      
              NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
              notificationManager.notify(111, notification);
          }
      
          @Override
          public void onMessageReceived(RemoteMessage remoteMessage) {
              super.onMessageReceived(remoteMessage);
          }
      
          @Override
          public void onDeletedMessages() {
              super.onDeletedMessages();
          }
      }
      

      【讨论】:

      • 不再工作:错误:MyFirebaseMessagingService 中的 handleIntent(Intent) 无法覆盖 FirebaseMessagingService 中的 handleIntent(Intent) 被覆盖的方法是最终的
      • 这对我仍然有效。 handleIntent() 总是被调用,前景和背景。所以在意图中我还包括了标题/正文。不再需要 onMessageReceived() 了。不要调用 .super 否则你会收到双重通知。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2020-05-02
      • 2019-01-26
      • 1970-01-01
      • 2018-02-13
      相关资源
      最近更新 更多