Add*_*dev 17 java notifications android
我正在测试可堆叠通知(Stacking Notifications文章).
我发现在某些情况下,notify()在运行android 4.X KitKat的设备中调用后,通知未显示.
为了简单地解决问题,我创建了这个模拟通知的代码(button1)和带有摘要的第二个通知(button2)
private final static int NOTIFICATION_ID_A=6;
private final static int NOTIFICATION_ID_B = 7;
private final static int NOTIFICATION_ID_SUMMARY = 8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNotif(NOTIFICATION_ID_A,false);
}
});
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNotif(NOTIFICATION_ID_B,false);
showNotif(NOTIFICATION_ID_SUMMARY,true);
}
});
}
private void showNotif(int notificationId,boolean groupSummary) {
CharSequence title="Title "+notificationId;
CharSequence message="Message "+notificationId;
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this);
notifBuilder.setSmallIcon(R.drawable.icon_notifications);
notifBuilder.setContentTitle(title);
notifBuilder.setContentText(message);
notifBuilder.setGroupSummary(groupSummary);
notifBuilder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));
notifBuilder.setGroup("group_" + 1);
NotificationManagerCompat.from(this).notify(notificationId, notifBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)
这个想法首先按下按钮1然后按下按钮2.它在android 5.0+中运行良好,显示第一个通知第一个和第二个按钮被点击时的摘要,但在Android 4.X中,button1没有显示任何内容.
错误在哪里?
谢谢
Geo*_*gan 15
对此的简短回答是,它似乎显示支持库不会自动支持KitKat设备上的堆叠通知.
既然你在这里要求启示我的发现是基于对运行Android 4.4.2的两个设备的测试.我也在使用AppCompat 23.1.1.
当您深入了解库的源代码时,您会发现当显示通知时,它将使用称为SideChannel或NotificationManager直接显示通知的内容.以下是NotificationManagerCompat.notify参考方法,显示:
public void notify(String tag, int id, Notification notification) {
// MY COMMENT: true when the notification has the extra
// NotificationCompatJellybean.EXTRA_USE_SIDE_CHANNEL set to true.
if (useSideChannelForNotification(notification)) {
pushSideChannelQueue(new NotifyTask(mContext.getPackageName(), id, tag, notification));
// Cancel this notification in notification manager if it just transitioned to being
// side channelled.
IMPL.cancelNotification(mNotificationManager, tag, id);
} else {
// MY COMMENT: this calls notificationManager.notify(id, notification); in the base implementation
IMPL.postNotification(mNotificationManager, tag, id, notification);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当您在未设置组的情况下显示通知时,将使用通知管理器显示通知,但如果设置了组,则会尝试使用辅助通道发送通知并取消使用通知管理器显示的任何通知在上面的方法中.
在设置组时使用侧通道的证明可以NotificationCompatKitKat.Builder在以下代码中找到:
if (groupKey != null) {
mExtras.putString(NotificationCompatJellybean.EXTRA_GROUP_KEY, groupKey);
if (groupSummary) {
mExtras.putBoolean(NotificationCompatJellybean.EXTRA_GROUP_SUMMARY, true);
} else {
mExtras.putBoolean(NotificationCompatJellybean.EXTRA_USE_SIDE_CHANNEL, true);
}
}
Run Code Online (Sandbox Code Playgroud)
在您查看使用该pushSideChannelQueue(...)方法显示通知时该方法的作用之前,这一切似乎都不是什么大问题SideChannel.
它最终寻找一个服务,可以处理android.support.BIND_NOTIFICATION_SIDE_CHANNEL默认情况下没有一个的操作,因此该方法只返回.这是导致通知永远不会显示的原因.
NotificationCompatSideChannelService兼容性库中有一个抽象类,如果你想编写自己的文档,你可以根据文档实现,SideChannelService但似乎你最好不要在KitKat和之前的设备中使用分组通知.
| 归档时间: |
|
| 查看次数: |
2079 次 |
| 最近记录: |