安卓通知管理器帮助

3

我正在研究Android通知。有没有办法将一个应用程序的所有通知显示为单个通知,并在Android通知窗口中显示数字。点击它会将用户重定向到应用程序中的一个视图,其中包含单独的通知。此外,是否有办法为每个通知设置活动,以便根据点击的通知将用户重定向到相应的活动。

1个回答

6
您可以将一个包含计数器的自定义布局附加到您的通知中。当事件发生时,增加计数器并更新通知,就像下面的示例一样: custom layout to your notificationupdate the notification。请保留HTML标记。
   Notification notification = new Notification(R.drawable.logo, name,    System.currentTimeMillis());

   RemoteViews contentView = new RemoteViews(appContext.getPackageName(), R.layout.custom_layout );

   contentView.setTextViewText(R.id.notification_text, Counter);

notification.contentView = contentView;

将活动附加到通知上:

        Intent notificationIntent = new Intent(appContext, MyActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt(...);
        notificationIntent.putExtras( bundle );
        notificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);           

        PendingIntent contentIntent = PendingIntent.getActivity( appContext, (int)System.currentTimeMillis(), notificationIntent, 0 );
        notification.contentIntent = contentIntent;

希望这能有所帮助。


1
谢谢Ackio。我会检查这个。如果Android有任何默认功能,那就更好了。我已经阅读了这篇文章:http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating他们没有解释如何更新通知。 - Dijo David
1
更新意味着使用相同的ID调用NotificationManager::notify。如果ID不同,则会添加新通知,否则将更新现有通知。 - ackio
在那篇文章中,我读到了这句话:“消息应用程序会更新现有的通知,以显示收到的新消息总数”。因此我相信这是可能的。 - Dijo David
最终我决定使用计数器来处理通知。最近的5条通知将会在通知管理器中显示。感谢您的帮助。 - Dijo David

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接