繁体   English   中英

前台中的Firebase通知

[英]Firebase notifications in the foreground

我遇到FireBase推送通知问题。 当我的应用程序在后台时,通知即将到来,但是当我的应用程序在前台时我没有收到通知,但在控制台中显示通知,这意味着通知在这里,但它没有显示在通知栏中。 你可以帮帮我吗?

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("OnMessage", "Received");
    super.onMessageReceived(remoteMessage);
    Log.d(TAG, "From " + remoteMessage.getFrom());
    Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
    String aa=remoteMessage.toString();
    Intent intent=new Intent(MyFirebaseMessagingService.this, MainActivity.class);
    intent.putExtra("msg", aa);
    sendNotification(remoteMessage);
    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setContentText(remoteMessage.getNotification().getBody());

    // remoteMessage.getNotification().getBody();

}


private void sendNotification(RemoteMessage remoteMessage) {

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

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

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setContentText(remoteMessage.getNotification().getBody())
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

编辑:

我刚刚注意到,当您从控制台发送通知并且您的应用程序位于前台时,不会调用FirebaseMessagingService的onMessageReceived()(截至目前)。 一种解决方案是您可以使用Firebase API发送推送通知,它将调用onMessageReceived(),无论应用程序位于前台还是后台。

如果您使用“通知”正文发送推送通知即

notification: {
  title: "notification title",
  icon: "ic_launcher",
  color: "#4E2AA5",
  body: "notification body"
}

要在onMessageReceived()中检索它,请使用以下内容:

String color = remoteMessage.getNotification().getColor();
String body = remoteMessage.getNotification().getBody();
String title = remoteMessage.getNotification().getTitle();
String icon = remoteMessage.getNotification().getIcon();

如果您的应用处于后台,Firebase将显示与您在推送中设置的数据相对应的通知,并且还会调用onMessageReceived()。 因此,如果您还编写了代码以在onMessageReceived()中显示自定义通知,您将看到2个通知。

要解决此问题,您可以使用一些“数据”键,如下所示:

data: {
  title: "notification title",
  body: "notification body"
}

并在onMessageReceived()中检索它,使用这样的东西:

Map<String, String> map = remoteMessage.getData();
for (Map.Entry<String, String> entry : map.entrySet()) {
   Log.d(TAG, entry.getKey() + "/" + entry.getValue());
}

然后,您可以构建自己的自定义通知,并仅在显示通知时从onMessageReceived()显示它。

PS如果有帮助,请投票。 我是stackoverflow的新手。

就像你说的那样,你正在接收消息,因为你在控制台中看到了日志消息,所以你已经正确处理了FCM部分,并且问题可能在于你创建了通知。

如果您查看通知创建代码,则缺少一些在Android上创建通知所需的元素 来自文档:

Notification对象必须包含以下内容:

  • 一个小图标,由setSmallIcon()设置
  • 标题,由setContentTitle()设置
  • 详细文本,由setContentText()设置

因此,我认为如果您将这些项目添加到通知创建中,您应该会在通知区域中看到通知。

Intent intent = new Intent(这是“你的班级”); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
           PendingIntent.FLAG_ONE_SHOT);

    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance);
        notificationManager.createNotificationChannel(notificationChannel);
        builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());
    } else {
        builder = new NotificationCompat.Builder(getApplicationContext());
    }

    builder = builder
            .setSmallIcon(R.mipmap.ic_app_icon)
            .setColor(ContextCompat.getColor(getApplicationContext(), R.color.picker_button_background_innactive))
            .setContentTitle((messageBody))
            .setTicker(title)
            .setContentText(message_body)
            .setDefaults(Notification.DEFAULT_ALL)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);
    notificationManager.notify(1, builder.build());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM