【问题标题】:Stack notifications in Kitkat (API 19) using setGroup() not working使用 setGroup() 在 Kitkat (API 19) 中堆栈通知不起作用
【发布时间】:2014-10-21 13:24:44
【问题描述】:

我有一个找不到答案的问题。我已经尝试过 AndroidDeveloper 教程,我已经在 stackoverflow 和 google 上进行了搜索,但是我的搜索技能很棒,或者没有答案,我认为可以解决我的问题。

当有多个新消息时,我想将消息通知堆叠到一个通知中。我可以为每条消息显示一个通知,但我不能发出堆栈/摘要通知。

我得到的最好的是: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 中进行测试。

我曾尝试使用 NotificationCompatManager 并收到多个通知:NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

我也尝试过使用 NotificationManager 得到相同的结果,即多个通知:

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();

然后我像这样触发通知:

notificationManager.notify(counter, notification);

每个通知的计数器都会递增,因此每个通知都有自己的 ID。

我也尝试过这种形式来构建和发送通知但没有成功:

  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 显然没有注意到我。我不明白为什么它不起作用或我应该如何使它起作用。

我的小组是这样的:

final static String mNoti = "mNoti";

唯一与我想要的远程接近的事情是对所有通知使用相同的 ID,以便新通知覆盖旧通知,并在构建通知时使用 setNumber(int)。但是,这并不能真正给我想要的结果,因为我认为我无法获得用户未处理的通知数量。或者我可以吗?

谁能帮助我达到我想要的目标?

【问题讨论】:

    标签: android notifications android-4.4-kitkat


    【解决方案1】:

    您在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。这意味着每个通知都将被视为摘要,因此将显示为新通知。

    为了解决您的问题,您可以在每次收到新通知时发送新的摘要。请注意,我不是 100% 确定这是 最佳 解决方案,但它解决了问题。 我创建了一个测试应用程序,它根据按钮点击添加通知:

    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:

    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查看邮件。

    我希望这会有所帮助:)

    【讨论】:

    • 我听从了您的建议并使用了 setGroupSummary 和 setGroup 并且它确实有效,尽管仅适用于 Android 5.0 及更高版本。 4.4 和 4.2 分别显示每个通知。这超出了这里的原始问题,所以我创建了一个新问题:stackoverflow.com/q/31483772/1204377
    • @AntonCherkashyn 你确定你已经包含了这一行:manager.cancelAll(); // Is this really needed? 吗?这是关闭旧通知而只打开摘要的“技巧”。
    • 我试过了,然后我只看到手机上的摘要通知,没错,但我的最终目标是在 Android Wear 上堆叠通知,为此我必须触发 N 个单独的通知 +每个 android 开发人员 giodelines 1 个摘要通知:developer.android.com/training/wearables/notifications/…。调用 manager.cancelAll();还会关闭有关 Android Wear 的通知。所以这是行不通的,因为这些单独的通知具有自定义操作/页面/等。
    • @AntonCherkashyn 啊,那是真的!我从来没有在 Android Wear 上测试过这个,取消然后添加一个摘要感觉就像一个丑陋的黑客,只是碰巧在手机上工作。 :)
    • 我找出了问题所在并更新了问题。我使用的是 NotificationManager 而不是 NotificationManagerCompat。切换到 compat 后,通知开始正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    相关资源
    最近更新 更多