对于我的应用程序,我使用一个通知ID来避免干扰用户的通知菜单。我的每个通知都有Ticker文本。当没有来自我的应用程序的通知,并且用户得到通知时,代码显示文本。如果通知已经存在,并且仅被更新,则不会显示代码标记文本。
我做了一个非常烦人的工作,在通知之前我取消了通知,但这最终导致了一个非常明显的滞后与振动。
目前,我是如何发出通知的:
mBuilder.setSmallIcon(R.drawable.actionbar_logo)
.setContentTitle(extras.getString("title"))
.setContentText(extras.getString("summary"))
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(extras.getString("extended_text"))
)
.setLights(Color.WHITE, NOTIF_LIGHT_INTERVAL, NOTIF_LIGHT_INTERVAL)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("mainActivityNotification", true);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 424242, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
final Notification notification = mBuilder.build();
notification.tickerText = extras.getString("title") + "\n" +
extras.getString("summary") + "\n" +
extras.getString("post_body");
mNotificationManager.notify(GATE_NOTIFICATION_ID, notification);发布于 2015-01-25 18:12:08
Android不会再次显示代码文本,如果它和以前一样的话。
您可以使用setTicker API of Notification.Builder类,而不是使用Notification对象构造代码标记文本,因为添加了Notification.Builder以便于构造通知。
如果您的意图是重新显示相同的代码文本(来自先前发布的通知),那么只需添加以下一行:
mNotificationManager.notify(GATE_NOTIFICATION_ID, mBuilder.build());在你发布你的第一份通知之后。
因此,完整的代码如下所示:
mBuilder.setSmallIcon(R.drawable.actionbar_logo)
.setContentTitle(extras.getString("title"))
.setContentText(extras.getString("summary"))
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(extras.getString("extended_text"))
) //consider using setTicker here
.setLights(Color.WHITE, NOTIF_LIGHT_INTERVAL, NOTIF_LIGHT_INTERVAL)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("mainActivityNotification", true);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 424242, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
final Notification notification = mBuilder.build();
//consider using setTicker of Notification.Builder
notification.tickerText = extras.getString("title") + "\n" +
extras.getString("summary") + "\n" +
extras.getString("post_body");
mNotificationManager.notify(GATE_NOTIFICATION_ID, notification);
//second time onward, add your changed content like setContentText, setTicker etc.
mNotificationManager.notify(GATE_NOTIFICATION_ID, mBuilder.build());https://stackoverflow.com/questions/27986937
复制相似问题