我正在尝试让本地通知像Hangouts一样工作。每次收到新的文本消息时,我希望会出现一个提醒通知。但是当有两个或更多未读通知时,我希望在Android通知栏中显示摘要通知。像这里描述的那样,似乎通过组合堆叠通知并添加组摘要可以实现这一目标。以下代码在Android 5.0和5.1上对我有效,但在Android 6.0上,当该组的摘要通知存在时,本地通知不会闹钟/以弹出视图显示。因此,只显示最初的通知。
public class MainActivity extends AppCompatActivity {
private Button _button = null;
final static String GROUP_KEY_EMAILS = "group_key_emails";
private int messageNum = 1;
private void CreateNotification() {
// Build the notification, setting the group appropriately
Notification headsUpNotification = new NotificationCompat.Builder(this)
.setContentTitle("Title")
.setContentText("New Message" + messageNum)
.setSmallIcon(R.drawable.pngreceivedtextmessage)
.setGroup(GROUP_KEY_EMAILS)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.build();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(messageNum, headsUpNotification);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.pngreceivedmessageicon);
if(messageNum > 1) {
// Create a summary notification since we have more than 1
Notification summaryNotification = new NotificationCompat.Builder(this)
.setContentTitle("Summary")
.setNumber(messageNum)
.setSmallIcon(R.drawable.pngreceivedtextmessage)
.setLargeIcon(largeIcon)
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
notificationManager.notify(0, summaryNotification);
}
messageNum++;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_button = (Button) findViewById(R.id.button);
_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CreateNotification();
}
});
}
我正在针对SDK 23进行开发,尝试了许多不同的组合但都没有起作用。有人知道如何生成摘要通知并仍然使弹出式通知(heads up notifications)工作吗?