【问题标题】:Firebase notifications in the foreground前台的 Firebase 通知
【发布时间】:2016-10-02 02:42:32
【问题描述】:

我在使用 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());
}

【问题讨论】:

    标签: android firebase push-notification google-cloud-messaging


    【解决方案1】:

    Intent intent = new Intent(this, "your class"); 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());
    

    【讨论】:

    • 工作对我来说很好
    【解决方案2】:

    编辑:

    我刚刚注意到,当您从控制台发送通知并且您的应用程序处于前台时,FirebaseMessagingService 的 onMessageReceived() 未被调用(截至目前)。一种解决方案是您可以使用Firebase APIs 发送推送通知,无论应用程序处于前台还是后台,它都会调用 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() 显示它,只有该通知才会显示。

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

    【讨论】:

    • 您是否注意到 sendNotification(RemoteMessage remoteMessage) 与 RemoteMessage 一起使用?但remoteMessage.getNotification().getBody() 实际上返回String。你给了不好的建议。
    【解决方案3】:

    正如您所说,您收到消息是因为您在控制台中看到日志消息,因此您已正确处理 FCM 部分,问题可能与您创建通知有关。

    如果您查看您的通知创建代码,您会缺少几个 required elements 在 Android 上创建通知。来自文档:

    通知对象必须包含以下内容:

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

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

    【讨论】:

    • 是的,这是个问题,谢谢伙计。还有一个问题,当我的后端向我发送通知并在 json 中设置标题时,谁来设置 setContentTitle()?
    • 通过通知消息 (json) 显示的通知与您在代码中创建和显示通知完全分开,就像您在此处所做的那样。如果您使用通知消息并且您的应用程序在后台,那么它将根据指定的 json 显示。否则,您将收到 onMessageReceived 回调,如果您希望发生这种情况,则由您的代码显示通知。
    猜你喜欢
    • 2019-08-05
    • 2020-02-14
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 2017-10-27
    • 2020-08-09
    • 2017-03-17
    • 1970-01-01
    相关资源
    最近更新 更多