堆叠通知无法正常工作无法正常、通知、工作

2023-09-03 22:16:24 作者:青衣剑客

我需要组成一个小组的通知。我做的例子在此输入链接的描述

但我的通知不进行分组。我做了什么错?

我的code:

 私有静态诠释的id = 0;
    最终静态字符串GROUP_KEY_GUEST =group_key_guest;


     私有静态无效generateNotification(上下文的背景下,字符串消息){

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(上下文);

            意向意图=新的意图(背景下,MyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(背景下,0,意向,PendingIntent.FLAG_CANCEL_CURRENT);

            通知通知=新NotificationCompat.Builder(上下文)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_stat_gcm)
                    .setLargeIcon(BitmapFactory.de codeResource(context.getResources(),R.drawable.ic_stat_gcm))
                    .setTicker(新邮件)
                    .setWhen(System.currentTimeMillis的())
                    .setAutoCancel(真)
                    .setContentTitle(消息)
                    .setContentText(消息)
                    .setGroup(GROUP_KEY_GUEST)
                    。建立();

            notificationManager.notify(ID ++,通知);
        }
 

方法调用:

  @覆盖
    保护无效的onMessage(上下文的背景下,意图意图){
        Log.d(TAG,Поступилосообщение:+ intent.getExtras());
        字符串消息= intent.getStringExtra(内容);
        generateNotification(背景下,消息);
    }
 

编辑,全code: 这是我的服务,我生成通知

 包com.managment.pavel.managmentgradle;

    公共类GCMIntentService扩展GCMBaseIntentService {
        私有静态诠释的id = 0;
        最终静态字符串GROUP_KEY_GUEST =group_key_guest;
        NotificationManagerCompat notificationManager;
        公共GCMIntentService(){
            超(SENDER_ID);
         notificationManager = NotificationManagerCompat.from(getApplicationContext());
        }

        @覆盖
        保护无效的onMessage(上下文的背景下,意图意图){
            字符串消息= intent.getStringExtra(内容);
            generateNotification(背景下,消息);
        }

        @覆盖
        保护无效的onError(上下文的背景下,String s)将{
            Toast.makeText(上下文中,错误,Toast.LENGTH_LONG).show();
        }

        @覆盖
        保护无效onRegistered(上下文的背景下,字符串registrationId){
            ServerUtilities.register(背景下,registrationId);
        }

        @覆盖
        保护无效onUnregistered(上下文的背景下,字符串registrationId){
            ServerUtilities.unregister(背景下,registrationId);
        }

        私人无效generateNotification(上下文的背景下,字符串消息){
            意向意图=新的意图(背景下,MyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(背景下,0,意向,PendingIntent.FLAG_CANCEL_CURRENT);

            通知通知=新NotificationCompat.Builder(上下文)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_stat_gcm)
                    .setLargeIcon(BitmapFactory.de codeResource(context.getResources(),R.drawable.ic_stat_gcm))
                    .setTicker(Новоесообщение)
                    .setWhen(System.currentTimeMillis的())
                    .setAutoCancel(真)
                    .setContentTitle(Сообщение)
                    .setContentText(消息)
                    .setGroup(GROUP_KEY_GUEST)
                    。建立();

            notificationManager.notify(ID ++,通知);
        }
    }
 
京东超医保百万医疗险 不保情况了解一下

解决方案

尝试在generateNotification初始化NotificationManagerCompat如果NotificationManagerCompat实例为null,并删除初始code形式的默认构造函数:

 私人无效generateNotification(上下文的背景下,字符串消息){
    如果(notificationManager == NULL){
       notificationManager = NotificationManagerCompat.from(上下文);
    }

    意向意图=新的意图(背景下,MyActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(背景下,0,意向,PendingIntent.FLAG_CANCEL_CURRENT);

    通知通知=新NotificationCompat.Builder(上下文)
    .setContentIntent(pendingIntent)
    .setSmallIcon(R.drawable.ic_stat_gcm)
    .setLargeIcon(BitmapFactory.de codeResource(context.getResources(),R.drawable.ic_stat_gcm))
    .setTicker(Новоесообщение)
    .setWhen(System.currentTimeMillis的())
    .setAutoCancel(真)
    .setContentTitle(Сообщение)
    .setContentText(消息)
    .setGroup(GROUP_KEY_GUEST)
    。建立();
    notificationManager.notify(ID ++,通知);
}
 

I need to form a group of notifications. I made the example enter link description here

but my notice are not grouped. what did I do wrong?

my code:

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

method call:

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

EDIT, full code: This is my service which I generate notifications

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

解决方案

Try initialize NotificationManagerCompat in generateNotification if NotificationManagerCompat instance is null and remove initialization code form default constructor :

private void generateNotification(Context context, String message) {
    if(notificationManager==null){
       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("Новое сообщение")
    .setWhen(System.currentTimeMillis())
    .setAutoCancel(true)
    .setContentTitle("Сообщение")
    .setContentText(message)
    .setGroup(GROUP_KEY_GUEST)
    .build();
    notificationManager.notify(id++, notification);
}