目前,我正在以以下方式发送通知:

    {
        "data" : {
            "sound": "default",
            "icon": "ic_app_launcher",
            "..." : "..."
            .
            .
            .
         }
    }



  E / fcmbundle:转储意图开始
  
  E / fcmbundle:[google.sent_time = ...]
  
  E / fcmbundle:[from = ...]
  
  E / fcmbundle:[icon = ic_app_launcher]
  
  E / fcmbundle:[声音=默认]
  
  E / fcmbundle:[google.message_id = 0:...]
  
  E / fcmbundle:[collapse_key = ...]
  
  E / fcmbundle:倾销意图结束


当应用程序在后台运行时,这是记录我从FCM获取的数据,单击通知时它将打开启动器活动。

我已经阅读了许多有关同一问题的答案,但对我不起作用。我正在发送数据消息,并在其中接收数据消息。声音和图标也。但是,不起作用。没有通知声音,图标显示为正方形。

I have also tried the single notification in json as:
{
    "to" : "...",
    "notification" : {
        "sound": "default",
        "icon" : "myicon"
    },
    "data" : {}
}


但这也不起作用。

该通知在后台出现,但没有声音,也没有显示图标。

当应用程序处于前台时,通知会正常显示图标,并根据需要发出声音通知。

谁能帮我这个忙吗?

最佳答案

对于前台和后台的通知,请考虑仅使用data有效负载。然后您的override中的onMessageReceived custom MessageService class方法是一种对我来说很好的解决方案。
notificationPayload = {"data": { "title": "your title" ,"body": "Your body"}}

public class CustomFirebaseMessagingService extends FirebaseMessagingService {
    final String CHANNEL_ID = "YOUR_CHANNEL_ID";//when targeting android-o

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    if (remoteMessage.getData().size() > 0) {
        sendNotification(remoteMessage);
    }
}


private void sendNotification(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();

    String title = data.get("title");
    String message = data.get("body");

    Intent resultIntent = new Intent(this, YOUR_TARGET_ACTIVITY.class);

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(
            this,
            0,
            resultIntent,
            PendingIntent.FLAG_ONE_SHOT
    );


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(message))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setAutoCancel(true);


    int notificationId = (int) System.currentTimeMillis();
    mBuilder.setSound(alarmSound);
    mBuilder.setContentIntent(resultPendingIntent);


    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (manager != null) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    "Human Readable channel id",
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.setShowBadge(true);
            manager.createNotificationChannel(channel);

        }

        manager.notify(notificationId, mBuilder.build());
    }
}


}

08-26 05:40