我想通过阅读这个问题的标题很难解释。我正在编写一个应用程序,它将已知服务器发送的环境因素警报(温度等)发送到GCM,然后GCM将其发送回电话。整个GCM运作良好。问题是通知到达时。当警报发生时(触发器),它被认为会向手机发送通知。然后单击警报将启动活动以显示警报。这没关系,但是如果等待点击有2个或更多警报,它将只处理一个,忽略其余的(" mensaje")。这是我在扩展 public static void showAlerts(Context context, String mensaje)
{
Intent notificationIntent = new Intent(context.getApplicationContext(), MainActivity.class);
notificationIntent.putExtra("mensaje", mensaje);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
Random r = new Random();
PendingIntent pendingIntent = PendingIntent.getActivity(context, r.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("Nueva alerta recibida")
.setSmallIcon(R.drawable.termometrorojo)
.setNumber(UtilidadesGCM.num_notificaciones++)
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setAutoCancel(true).build();
notificationManager.notify(0, notification);
}
的类中的通知的样子。
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String nuevaAlerta = intent.getExtras().getString("mensaje");
procesaAlerta(nuevaAlerta);
//mDisplay.append(nuevaAlerta + "\n");
}
};
public void procesaAlerta (String alerta)
{
Intent intent = new Intent(this, Alertas.class);
intent.putExtra("mensaje" , alerta);
startActivity(intent);
}
然后在MainActivity中,我有代码来处理它,并打开活动以显示警报
{{1}}
Alertas类将解析消息并将其显示在其活动中,但只会执行一次。如果要读取的警报堆叠超过2个,则只处理一个。如果有,它可以正常工作。对不起,如果我没有更好的解释,但很难没有显示所有的代码。 谢谢!
答案 0 :(得分:1)
尝试写这一行
notificationManager.notify( new Random().nextInt(), notification);
而不是
notificationManager.notify(0, notification);
您的通知ID每次都相同,因此您的上一个通知仅适用。每个新的通知ID都被id 0替换。所以我使用随机id而不是固定id 0.我认为上面的代码将适合你