为啥通知组在android中不起作用?我做错了啥?

Posted

技术标签:

【中文标题】为啥通知组在android中不起作用?我做错了啥?【英文标题】:Why Notification group not working in android? what I am doing wrong?为什么通知组在android中不起作用?我做错了什么? 【发布时间】:2019-11-28 09:46:16 【问题描述】:

在这里我附加了我的通知实用程序类,我试图将我的通知分组,但每次我收到单独的通知时,我都会传递所有通知唯一的通知 ID。当我想从面板中删除特定通知时使用它。

public class NotificationUtils 

private static final String TAG = "NotificationUtilss";

private static final String CHANNEL_ID = "channel";
private static final String CHANNEL_NAME = "my_channel";
private static final String GROUP_KEY_PN = "com.demo.app.PN";
private static final String URL = "url";
private static final String ACTIVITY = "activity";
Map<String, Class> activityMap = new HashMap<>();
private Context mContext;

public NotificationUtils(Context mContext) 
    this.mContext = mContext;
    //Populate activity map
    activityMap.put("Activity1", Activity1.class);
    activityMap.put("Activity2", Activity2.class);
    activityMap.put("Activity3", Activity3.class);


public void displayNotification(NotificationVO notificationVO, Intent resultIntent) 
    
        String notification_id = notificationVO.getNotification_id();
        String message = notificationVO.getMessage();
        String title = notificationVO.getTitle();
        String iconUrl = notificationVO.getIconUrl();
        String action = notificationVO.getAction();
        String destination = notificationVO.getActionDestination();
        Bitmap iconBitMap = null;
        if (iconUrl != null) 
            iconBitMap = getBitmapFromURL(iconUrl);
        
        final int icon = R.drawable.logo;

        PendingIntent resultPendingIntent;

        if (URL.equals(action)) 
            Intent notificationIntent = new Intent(Intent.ACTION_VIEW, 
Uri.parse(destination));
            resultPendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 
0);
         else if (ACTIVITY.equals(action) && activityMap.containsKey(destination)) 
            resultIntent = new Intent(mContext, activityMap.get(destination));
            resultPendingIntent =
                    PendingIntent.getActivity(
                            mContext,
                            0,
                            resultIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );
         else 
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
Intent.FLAG_ACTIVITY_SINGLE_TOP);
            resultPendingIntent =
                    PendingIntent.getActivity(
                            mContext,
                            0,
                            resultIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );
        

        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, 
CHANNEL_ID);
        Notification notification;
        if (iconBitMap == null) 
            //When Inbox Style is applied, user can expand the notification
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.addLine(message);
            inboxStyle.setBigContentTitle(title);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) 
                notification = mBuilder
                        .setTicker(title)
                        .setAutoCancel(true)
                        .setContentTitle(title)
                        .setContentText(message)
                        .setContentIntent(resultPendingIntent)
                        .setStyle(inboxStyle)
                        .setWhen(new Date().getTime())
                        .setSmallIcon(R.drawable.logo)
                        .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), 
icon))
                        .setGroup(GROUP_KEY_PN)
                        .setGroupSummary(true)
                        .build();
            
        
        NotificationManager notificationManager = (NotificationManager) 
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, 
NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        
        NotificationManagerCompat.from(mContext).notify(Integer.parseInt(notification_id), 
notification);
    


我需要帮助,仅应用通知样式是对通知进行分组,但应用组和组摘要不起作用。

【问题讨论】:

您是否尝试过删除组摘要? @ShripadJadhav 是的,我试过了,但它不起作用 【参考方案1】:

您需要设置一个唯一标识符,因为您使用setGroupSummary true,并且还需要以不同的方式创建摘要和实际通知。如果你想要一个摘要,你需要:

val SUMMARY_ID = 0 // This is unique
notify(SUMMARY_ID, summaryNotification)
val SUMMARY_ID = 0
val GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL"

val newMessageNotification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notify_email_status)
        .setContentTitle(emailObject1.getSummary())
        .setContentText("You will not believe...")
        .setGroup(GROUP_KEY_WORK_EMAIL)
        .build()

val summaryNotification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
        .setContentTitle(emailObject.getSummary())
        //set content text to support devices running API level < 24
        .setContentText("Two new messages")
        .setSmallIcon(R.drawable.ic_notify_summary_status)
        //build summary info into InboxStyle template
        .setStyle(NotificationCompat.InboxStyle()
                .addLine("Alex Faarborg Check this out")
                .addLine("Jeff Chang Launch Party")
                .setBigContentTitle("2 new messages")
                .setSummaryText("janedoe@example.com"))
        //specify which group this notification belongs to
        .setGroup(GROUP_KEY_WORK_EMAIL)
        //set this notification as the summary for the group
        .setGroupSummary(true)
        .build()

NotificationManagerCompat.from(this).apply 
    notify(emailNotificationId, newMessageNotification)
    notify(SUMMARY_ID, summaryNotification)

查看documentation了解更多信息。

【讨论】:

添加 summary_id 会移除之前的通知 这就是为什么我告诉你检查完整的文档,如果你检查文档,你会发现你必须创建作为一个对象到达的通知,以及作为另一个对象的摘要。我将从这里的文档中复制粘贴示例,但我不想在这里搞得太乱,请查看! 谢谢你的兄弟,它的工作就像一个魅力.. 在你发表评论之前,我详细看过文件。非常感谢你的帮助。从下次开始,我会确保正确阅读文档。 你能帮我解决这个问题吗? ***.com/questions/58976433/…【参考方案2】:

如果您不想要摘要,请尝试将其删除或使用摘要结帐查看开发人员documentation 的示例

【讨论】:

以上是关于为啥通知组在android中不起作用?我做错了啥?的主要内容,如果未能解决你的问题,请参考以下文章

Android OS 2.2 权限:我完全不知道为啥这段简单的代码不起作用。我究竟做错了啥?

波形写入功能不起作用,我做错了啥?

协议和代表不起作用..我做错了啥?

iOS推送通知我做错了啥? [关闭]

带有颜色资源的Android LinearLayout:我做错了啥?

为啥 colgroup/col 在 Chrome 中不起作用