【发布时间】: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