【问题标题】:Notification logo not shown when app not running (FCM)应用未运行时未显示通知徽标 (FCM)
【发布时间】:2019-05-23 19:11:47
【问题描述】:

所以我已经为通知实施了 FCM。 因此,我们还可以使用 Firebase Cloud Messaging 发送一些数据以及通知。 在这里,我创建了一个频道并将设备注册到“一般”主题。

这是我的代码:

包 com.femindharamshi.fcmtrial;

import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Map m = remoteMessage.getData();
        showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), m);

        Class me = remoteMessage.getClass();
        Log.d("MessageDateRecieved", ""+me.toString());

    }

    public void showNotification(String title, String message, Map m) {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotifications")
                .setContentTitle(title)
                .setSmallIcon(R.drawable.small_logo)
                .setAutoCancel(true)
                .setContentText(message);

        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        manager.notify(999, builder.build());

    }
}

现在我有以下两个问题:

  1. 当应用程序未运行/终止时,我的 res/drawable 文件夹中的通知徽标 (R.drawable.small_logo) 不会显示,而是显示一个灰色圆圈。我该如何解决这个问题。
  2. 如果我通过此消息传递数据,我可以在地图中捕获它。现在成像应用程序没有运行,我想将这些数据保存在 SharedPreferences 中,那怎么可能?

【问题讨论】:

  • 由于图标的形状而显示灰色图标。尝试使用不同形状的图标,它会起作用。关于在单击通知时保存数据,与通知一起发送的值将作为意图传递给您的启动器活动,您可以使用getextra 提取它们
  • 如果应用未运行,则不会调用您的服务,因此您无法创建自定义通知。

标签: android push-notification notifications firebase-cloud-messaging sharedpreferences


【解决方案1】:

当您的应用程序未运行时显示的通知图标只能是单色的,因此应使用具有透明度的图标作为形状和颜色,因为它可以单独提供。看起来像AndroidManifest.xml 中的application 标记内的这些行。

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

【讨论】:

  • 太棒了!所以通过这样做,即使应用程序没有运行,我也可以收到带有我的徽标的通知?我提供的颜色可以是任何东西?
  • 是的,当您的应用程序未运行时,您将获得用于通知的自定义单色徽标。它将在状态栏中使用默认颜色,但是当您展开通知时,徽标将按照您提供的颜色显示。
猜你喜欢
  • 1970-01-01
  • 2020-08-13
  • 2017-06-14
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-14
  • 2016-12-31
相关资源
最近更新 更多