【问题标题】:Stacking notifications from GCM is not working with setGroup来自 GCM 的堆叠通知不适用于 setGroup
【发布时间】:2016-05-09 13:51:16
【问题描述】:

我在一个扩展 GcmListenerService 的类中有以下通知逻辑,并在一个通知到达时被调用。然后,当单击时,应用程序会将您带到 MainActivity,在该处正确显示通知。

public static void mostrarAvisoBarraEstado(Context context, String alerts)
{
    Intent notificationIntent = new Intent(context.getApplicationContext(), MainActivity.class);
    notificationIntent.putExtra("alerts", alerts);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, new Random().nextInt(),
            notificationIntent, 0);

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

    Notification notification = new NotificationCompat.Builder(context)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentText("Alert received")
            .setSmallIcon(R.drawable.nubeazul)
            .setOnlyAlertOnce(true)
            .setContentIntent(pendingIntent)
            .setWhen(System.currentTimeMillis())
            .setGroup(GRUPO_ALERTAS)
            .setGroupSummary(true)
            .setAutoCancel(true)
            .build();

    //notificationManager.notify(0, notification);
    notificationManager.notify (new Random().nextInt(), notification);
}

所以,现在,每一个都是单独显示的,如果它们堆积起来,结果是相当丑陋的,所有的通知栏都充满了小图标。既然我对Android有点陌生,你们能帮忙提供一个优雅的解决方案吗?非常感谢!

今天添加了新内容!

如果我随机取出通知,留下notificationManager.notify(0, notification); 之类的东西,我将只收到一个通知,但没有其他通知,然后当它启动 MainActivity(它的 onResume() 方法)时,它只会显示一个通知,而所有单击一个通知时,“堆积的”只会被丢弃。我想要实现的是在保持干净显示的同时,即:所有 GCM 的一个组通知,如果我单击该组,我将通过 Alerts.class 显示每个通知(类似于循环通知,并为每个活动启动警报。

   @Override
protected void onResume()
{
    super.onResume();

    if (getIntent().hasExtra("alerts"))
    {
        Bundle extras = getIntent().getExtras();
        Intent intent = new Intent(this, Alerts.class);
        intent.putExtra("alerts" , extras.getString("alerts"));
        startActivity(intent);
        getIntent().removeExtra("alerts");
    }
}

然后 Alerts 类将很好地显示它所做的警报,但每个通知一个。

【问题讨论】:

  • 你能提供一些你得到的截图吗?他们喜欢只是堆积吗?
  • 是的,应用程序的图标只是针对每个通知重复,例如,对于您在 gmail 中收到的每条消息,您都会获得一个单独的 gmail 图标和通知
  • 我会试试你的代码,然后告诉你我发现了什么。 :)

标签: android google-cloud-messaging android-notifications


【解决方案1】:

所以我尝试了您的代码并设法重现了您遇到的情况(我只是手动创建了虚拟通知)。所以Notifications 堆积的原因是因为您在notificationManager.notify() 中传递的id 彼此不同。至于到目前为止我在NotificationNotificationManagers 行为中观察到的情况,notify() 中指示的 id 有点代表Notification 的 id 位置/位置(不知道叫什么),即在NotificationManager 下,不是Notification 本身的ID。

NotificationManager 想象成一个数组或列表。例如,如果您在状态栏上有 3 个可见通知:

Notification 1Notification 2Notification 3,其id分别为:0、1、2。

如果您生成 new Notification 4 然后调用 notify 将其作为参数传递,并且 id 1,则当前将在 NotificationManager 中显示的通知将导致某些结果像这样:

Notification 1Notification 4Notification 3,它们的id分别为:0、1、2。

所以你的通知堆积起来而不是组合在一起的原因是因为你在通过new Random().nextInt()时调用notify()时最终得到不同的ID。

根据我认为您的目标行为 - 将应用程序中的通知合并为一个 - 理解时实现很简单,但我认为它仍然有点棘手。就像您必须首先检查是否已经有超过 1 个通知,然后如果是,则创建一个包含详细信息的摘要通知并单独显示它(注意 id ;))和所有那些其他的东西。我发现了这个blog,尽管我认为它可能会对你有所帮助。或者您可以直接查看Stacking Notifications 上的官方文档。

所以最重要的是,当涉及到您的应用通知时,只需使用单个 id 来传递 notify()。希望这可以帮助。干杯! :D

【讨论】:

  • 我添加了更多信息,因为我没有看到解决方案。非常感谢!
  • 看到了新的东西。如上所述,您所针对的行为需要进一步编码。尝试查看我链接的博客,我认为他们有一个简单的示例,您可以尝试,然后修改。干杯! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
相关资源
最近更新 更多