android - 堆叠通知不起作用

标签 android android-notifications

我需要形成一组通知。我做了例子 enter link description here

但是我的通知没有分组。我做错了什么?

我的代码:

private static int id =0;
    final static String GROUP_KEY_GUEST = "group_key_guest";


     private static void generateNotification(Context context, String message) {

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

            Intent intent = new Intent(context,MyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

            Notification notification   = new NotificationCompat.Builder(context)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_stat_gcm)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
                    .setTicker("New Message")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentTitle("Message")
                    .setContentText(message)
                    .setGroup(GROUP_KEY_GUEST)
                    .build();

            notificationManager.notify(id++, notification);
        }

方法调用:

 @Override
    protected void onMessage(Context context, Intent intent) {
        Log.d(TAG, "Поступило сообщение: " + intent.getExtras());
        String message = intent.getStringExtra("content");
        generateNotification(context, message);
    }

编辑,完整代码: 这是我生成通知的服务

 package com.managment.pavel.managmentgradle;

    public class GCMIntentService extends GCMBaseIntentService {
        private static int id =0;
        final static String GROUP_KEY_GUEST = "group_key_guest";
        NotificationManagerCompat notificationManager;
        public GCMIntentService() {
            super(SENDER_ID);
         notificationManager = NotificationManagerCompat.from(getApplicationContext());
        }

        @Override
        protected void onMessage(Context context, Intent intent) {
            String message = intent.getStringExtra("content");
            generateNotification(context, message);
        }

        @Override
        protected void onError(Context context, String s) {
            Toast.makeText(context,"Error",Toast.LENGTH_LONG).show();
        }

        @Override
        protected void onRegistered(Context context, String registrationId) {
            ServerUtilities.register(context, registrationId);
        }

        @Override
        protected void onUnregistered(Context context, String registrationId) {
            ServerUtilities.unregister(context, registrationId);
        }

        private void generateNotification(Context context, String message) {
            Intent intent = new Intent(context,MyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

            Notification notification   = new NotificationCompat.Builder(context)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_stat_gcm)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
                    .setTicker("Новое сообщение")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentTitle("Сообщение")
                    .setContentText(message)
                    .setGroup(GROUP_KEY_GUEST)
                    .build();

            notificationManager.notify(id++, notification);
        }
    }

最佳答案

您必须使用之前的详细信息创建摘要通知。 可见的是这个摘要通知,而不是之前的通知。它是这样的:

private static int id =0;
private static int unread_notif = 0;
final static String GROUP_KEY_GUEST = "group_key_guest";

private static void generateNotification(Context context, String message) {

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

        Intent intent = new Intent(context,MyActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Notification notification   = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_stat_gcm)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
                .setTicker("New Message")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("Message")
                .setContentText(message)
                .setGroup(GROUP_KEY_GUEST)
                .build();

        notificationManager.notify(id++, notification);

        unread_notif++;

        if (unread_notif>1) {
           Notification summaryNotification = new NotificationCompat.Builder(mContext)
                .setContentTitle("Your summary message")
                .setSmallIcon(R.drawable.ic_small_icon)
                .setLargeIcon(largeIcon)
                .setStyle(new NotificationCompat.InboxStyle()
                    .addLine("Details about your first notification")
                    .addLine("Details about your second notification")
                .setBigContentTitle(Integer.toString(unread_notif)+" new notifications")
                .setSummaryText("More details in app"))
                .setGroup(GROUP_KEY_GUEST)
                .setGroupSummary(true)
                .build();

            notificationManager.notify(id++, summaryNotification);
        }
    }

请参阅 Add a Summary Notification 中的文档

关于android - 堆叠通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27309037/

相关文章:

android - 如何在 flutter 的 View 之间发送和接收参数

java - 找出两个时间之间最接近的时间

Android O : Service. stopForeground 等效

android - 以编程方式结束正在进行的通知 - Android

android - 如何在 For 循环中添加随机颜色

java - 如何将字符串写入txt文件并将其发送到服务器

android - Android 可以在折叠的通知中显示操作图标吗?

Android 在 GCM 上注册设备导致 SERVICE_NOT_AVAILABLE

Android Studio 2.1.2 和 Android 6 : Error while installing restart patches

android - 如何将 android Notification 转换为 NotificationCompat