问题描述
我有一个找不到答案的问题.我已经尝试过AndroidDeveloper教程,我已经在此处在stackoverflow和google上进行了搜索,但是我的搜索技能太强了,或者我认为没有答案,这回答了我的问题.
I have a problem that I can't find the answer to. I have tried the AndroidDeveloper tutorials, I have searched here on stackoverflow and on google but either my search-skills are awefull or there's no answer I think answers my problem.
我想将所有多条新消息中的消息通知堆叠到一个通知中.我可以使每条消息都显示一个通知,但不能发出堆栈/摘要通知.
I want to stack message notifications into one notification for all new messages when there are more than one. I can make a notification appear for each message but I cannot make a stack/summary notification.
我得到的最好的是: http://developer.android .com/images/android-5.0/notifications/Summarise_Dont.png
我想要的是: http://developer.android. com/images/android-5.0/notifications/Summarise_Do.png
我正在使用API 19,并且不必向后兼容.在AndroidStudio中编写我的代码,并在Genymotion中进行测试.
I am using API 19 and it does not have to be back-compatible. Writign my code in AndroidStudio and testing in Genymotion.
我已尝试使用NotificationCompatManager来获得多个通知:NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
I have tried using NotificationCompatManager with the result of having multiple notifications: NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
我也尝试过使用具有相同结果(即多个通知)的NotificationManager:
I have also tried using NotificationManager with the same result, that is multiple notifications:
NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
我的通知如下:
Notification notification = new NotificationCompat.Builder(this) .setContentTitle( "New message from... ") .setContentText("Message:...") .setSmallIcon(R.drawable.icon) .setContentIntent(pIntent) .setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION)) .setGroup(mNoti) .setGroupSummary(true) .setAutoCancel(true) .build();
Notification notification = new NotificationCompat.Builder(this) .setContentTitle( "New message from... ") .setContentText("Message:...") .setSmallIcon(R.drawable.icon) .setContentIntent(pIntent) .setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION)) .setGroup(mNoti) .setGroupSummary(true) .setAutoCancel(true) .build();
然后我发出这样的通知:
And I fire the notification like this:
notificationManager.notify(counter, notification);
每个通知的计数器都会增加,因此每个通知都有自己的ID.
Where counter increments for each notification so each notification has its own ID.
我也尝试过使用此表单来构建和发出通知而不会成功:
I have also tried this form for building and fiering the notification without succsess:
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("message from...")
.setContentText("message...")
.setContentIntent(pIntent)
.setAutoCancel(true)
.setGroup(mNoti)
.setGroupSummary(true)
.setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION));
notificationManager.notify(counter, notificationBuilder.build());
setGroup显然不适合我.我不明白为什么它不起作用或我应该如何使其起作用.
setGroup obviously does noting for me. I don't understand why it doesn't work or how I'm supposed to make it work.
我的小组看起来像这样:
my group look like this:
final static String mNoti = "mNoti";
唯一接近我想要的东西是对所有通知使用相同的ID,以便新通知覆盖旧通知,并在构建通知时使用setNumber(int).但是,这并没有真正给我想要的结果,因为我认为我无法获得用户未处理的通知数量.可以吗
The only thing that comes remotely close to what I want is using the same ID for all notifications so that the new notifications overrides the old ones, and using setNumber(int) while building the notification. However this doesn't truely give me the result I want because I don't think I can get the number of notifications not handled by the user. Or can I?
有人可以帮助我达到我的期望目标吗?
Can anyone help me arrive at my desired goal?
推荐答案
获得结果的原因如 http://developer.android.com/images/android-5.0/notifications/Summarise_Dont.png 的原因是,您将所有通知的组摘要设置为true.在API文档中,它描述为:Set this notification to be the group summary for a group of notifications
.这意味着每个通知都将被视为摘要,因此将被显示为新通知.
The reason you are getting results as in http://developer.android.com/images/android-5.0/notifications/Summarise_Dont.png is because you set group summary to true on all your notifications. In the API docs it is described as: Set this notification to be the group summary for a group of notifications
. This means that every notification will be treated as a summary, and thus will be displayed as a new notification.
为解决您的问题,您可以在每次收到新的通知时发送新的摘要.请注意,我不是100%肯定这是 best 的解决方案,但是它可以解决问题.我创建了一个测试应用程序,该应用程序添加了基于按钮单击的通知:
To solve your problem, you can send a new summary every time you get a new notification. Note that I am not 100% sure that this is the best solution, but it solves the problem.I created a test application which adds notifications based on button clicks:
public class MyActivity extends Activity {
private final static String GROUP_KEY_MESSAGES = "group_key_messages";
public static int notificationId = 0;
private NotificationManagerCompat manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
manager = NotificationManagerCompat.from(this);
manager.cancelAll();
}
public void addNotification(View v) {
notificationId++; // Increment ID
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Intent viewIntent = new Intent(this, ShowNotificationActivity.class);
viewIntent.putExtra("notificationId", notificationId); // Put the notificationId as a variable for debugging purposes
PendingIntent viewPendingIntent = PendingIntent.getActivity(this, notificationId, viewIntent, 0); // Intent to next activity
// Group notification that will be visible on the phone
Notification summary = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentIntent(viewPendingIntent)
.setContentTitle("New notifications")
.setContentText("You have "+notificationId+" new messages")
.setLargeIcon(icon)
.setGroup(GROUP_KEY_MESSAGES)
.setGroupSummary(true)
.build();
manager.cancelAll(); // Is this really needed?
manager.notify(notificationId, summary); // Show this summary
}
}
ShowNotificationActivity:
ShowNotificationActivity:
public class ShowNotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_notification);
Intent intent = getIntent();
int counter = intent.getIntExtra("COUNTER", -57);
Log.d("COUNTER", ""+counter);
TextView view = (TextView)(findViewById(R.id.textView));
view.setText("You have just read "+counter+" messages");
MyActivity.notificationId = 0; // Reset ID. Should not be done like this, but used to show functionality.
}
}
第一个活动MyActivity
用于处理通知.我想这主要是在接收器中完成的. MyActivity中的按钮触发addNotification,它会推送新的通知摘要.在添加摘要之前,所有旧摘要都将被取消.单击摘要时,您会进入名为ShowNotificationActivty
的启动活动,其中显示了数据.例如,如果这是电子邮件应用程序,则MyActivity
将是某种将电子邮件保存到数据库的电子邮件接收者.可以将notificationId设置为数据库中保存的某些ID.单击通知后,可以根据notificationId查看电子邮件.
The first activity MyActivity
is used to handle notifications. I guess that this is mostly done in receivers though. A button in MyActivity triggers addNotification which pushes the new notification summary. Before adding the summary all old summaries are cancelled. when clicking on the summary you get to the startup activity, called ShowNotificationActivty
where data is shown. If this was, for instance, an email app, MyActivity
would be some sort of email receiver which saved the email to a database. The notificationId could be set to some id saved in the database. When the notifications are clicked, the email can be viewed based on notificationId.
我希望这会有所帮助:)
I hope this helps :)
这篇关于使用setGroup()的Kitkat(API 19)中的堆栈通知不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!