【问题标题】:Android notification badge dot not showing for custom firebase push notification自定义 Firebase 推送通知未显示 Android 通知徽章点
【发布时间】:2020-10-09 06:49:22
【问题描述】:

我已经为 firebase 推送通知实现了自定义视图。对于自定义视图,我们需要从 push Json 中删除“通知”键,以便即使在应用程序关闭时也可以处理它,如下所示:

{  
  "data": {
    "detail": {
     }
  },
  "to": "" 
}

为了创建自定义通知,我使用了以下代码:

private void generateNotification(String title, String message, Intent intent) {
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        String channelId = getString(R.string.default_notification_channel_id);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationCount, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        /*
        * Custom notification layout
        * */
        String notificationHeaderText = getResources().getString(R.string.app_name) + " \u2022 "
                + DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME);
        RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.view_collapsed_notification);
        collapsedView.setTextViewText(R.id.timestamp, notificationHeaderText);
        collapsedView.setTextViewText(R.id.content_title, title);
        collapsedView.setTextViewText(R.id.content_text, message);

        RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.view_expanded_notification);
        expandedView.setTextViewText(R.id.timestamp, notificationHeaderText);
        expandedView.setTextViewText(R.id.content_title, title);
        expandedView.setTextViewText(R.id.notification_message, message);

        Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.footer_click);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_inkclick_logo_colored)
                .setSound(soundUri)
                .setGroup(GROUP_KEY_INKCLICK)
                .setAutoCancel(true)
                .setGroupSummary(true)
                .setCustomContentView(collapsedView)
                .setCustomBigContentView(expandedView)
                .setContentIntent(pendingIntent);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (manager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        getResources().getString(R.string.default_notification_channel_id), NotificationManager.IMPORTANCE_HIGH);
                channel.enableLights(true);
                channel.setLightColor(Color.MAGENTA);
                channel.setVibrationPattern(new long[]{0, 1000/*, 500, 1000*/});
                channel.enableVibration(true);
                channel.setShowBadge(true);
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .build();
                channel.setSound(soundUri, audioAttributes);
                manager.createNotificationChannel(channel);
            }
            manager.notify(0, notificationBuilder.build());
        }
        else {
            manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) {
                manager.notify(0, notificationBuilder.build());
            }

        }
        notificationCount += 1;
    }

在阅读官方通知徽章文档here 以及stackoverflow 上的其他答案(如thisthis)后,我添加了channel.setShowBadge(true);

我也尝试卸载应用程序并重新启动设备,但未显示徽章。 设备在 API 28(Pie) 上运行。

【问题讨论】:

  • 你有什么运气让它工作吗?我遇到了同样的问题。通知本身显示正常,但无论我尝试什么,通知点都不会出现。
  • @etrado 我还没有找到任何解决方案。仍在寻找答案。

标签: android firebase firebase-cloud-messaging custom-notification


【解决方案1】:

您需要指定标题或文本(setContentTitlesetContentText)。因为 Android SDK 使用它来描述当您长按应用程序图标时出现的窗口中的通知。我不知道为什么文档中没有指定,但它是这样工作的。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

您可以像这样设置 Android 通知徽章的样式:

.setStyle(new NotificationCompat.DecoratedCustomViewStyle())

整体代码可以是这样的:

  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_inkclick_logo_colored)
                .setSound(soundUri)
                .setGroup(GROUP_KEY_INKCLICK)
                .setAutoCancel(true)
                .setGroupSummary(true)
                .setCustomContentView(collapsedView)
                .setCustomBigContentView(expandedView)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setContentIntent(pendingIntent);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);

【讨论】:

  • 纯代码答案是低质量的答案。
  • 哦,好吧......希望你能得到你的答案,如果没有,请告诉我......我会向你解释
  • 我已编辑您的答案,使其更清晰。下次请注意,如果您要粘贴一些代码,请务必添加一些上下文。
【解决方案3】:

您可以通过添加以下行来设置样式

.setStyle(new NotificationCompat.DecoratedCustomViewStyle())

添加此代码

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_inkclick_logo_colored)
                .setSound(soundUri)
                .setGroup(GROUP_KEY_INKCLICK)
                .setAutoCancel(true)
                .setGroupSummary(true)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle()) 
                .setCustomContentView(collapsedView)
                .setCustomBigContentView(expandedView)
                .setContentIntent(pendingIntent);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多