Android:通知动作点击非统一行为

Posted

技术标签:

【中文标题】Android:通知动作点击非统一行为【英文标题】:Android: Notification action click non-uniform behavior 【发布时间】:2017-02-08 14:28:41 【问题描述】:

我在使用 firebase 进行通知时遇到通知问题。

场景是 - 当收到通知时,它有两个操作按钮,因此通知将重定向,但我没有执行任何操作,并且我收到了第二个通知,在这种情况下,我为第二个通知设置的意图将丢失。仅当两个通知都重定向到相同的活动但执行不同的操作时才会发生。

这是我的创建通知代码。

private void createNotification(Map<String, Object> dataMap) 

        final String type = dataMap.get("type") + "";
        if (CommonUtil.isEmpty(type)) 
            return;
        

        final FirebaseService.NotificationType notificationType = FirebaseService.NotificationType.valueOf(type);
        final String alert = dataMap.get("alert") + "";
        String title = dataMap.get("title") + "";

        String notificationMessage = alert;


        notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        unreadNotifications.add(0, notificationMessage);
        android.support.v4.app.NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        if (unreadNotifications.size() > 1) 
            inboxStyle.setBigContentTitle("MyApp");
            for (String unreadNotification : unreadNotifications) 
                inboxStyle.addLine(unreadNotification);
            
            inboxStyle.setSummaryText(unreadNotifications.size() + " new notifications.");
        

        Intent intent;
        intent = new Intent(app, LandingActivity.class);
        if (dataMap.containsKey("code")) 
            intent.putExtra(IntentExtras.CODE_FROM_NOTIFICATION, dataMap.get("code") + "");
        

        intent.setAction(System.currentTimeMillis() + "");
        PendingIntent pendingIntent = TaskStackBuilder.create(app)
                .addNextIntent(intent)
                .getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
        int icon_one = 0, icon_two = 0;
        String actionTextOne = null, actionTextTwo = null;
        PendingIntent actionOne = null, actionTwo = null;
        Intent intentActionOne = null, intentActionTwo = null;

        if (notificationType.equals(NotificationType.REQUEST_RECEIVED)) 
            icon_one = R.drawable.cancel;
            icon_two = R.drawable.notification_track;

            actionTextOne = "Edit";
            actionTextTwo = "Cancel";

            intentActionOne = new Intent(app, LandingActivity.class);
            intentActionOne.putExtra(IntentExtras.CODE_FROM_NOTIFICATION, dataMap.get("code") + "");
            intentActionOne.putExtra(IntentExtras.EDIT, true);

            intentActionTwo = new Intent(app, LandingActivity.class);
            intentActionTwo.putExtra(IntentExtras.CODE_FROM_NOTIFICATION, dataMap.get("code") + "");
            intentActionTwo.putExtra(IntentExtras.CANCEL, true);

         else if (notificationType.equals(NotificationType.REQUEST_CANCELED)) 
            icon_one = R.drawable.reschedule;
            icon_two = R.drawable.help;

            actionTextOne = "Reschedule";
            actionTextTwo = "Help?";

            intentActionOne = new Intent(app, LandingActivity.class);
            intentActionOne.putExtra(IntentExtras.RESCHEDULE, true);

            intentActionTwo = new Intent(app, LandingActivity.class);
            intentActionTwo.putExtra(IntentExtras.HELP, true);
        

        NotificationCompat.Builder builder = new NotificationCompat.Builder(app)
                .setSmallIcon(R.drawable.status_bar_icon)
                .setLargeIcon(BitmapFactory.decodeResource(app.getResources(), R.drawable.notifications))
                .setContentTitle(title)
                .setContentText(alert)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setSound(notificationSound)
                .setLights(Color.CYAN, 3000, 3000)
                .setPriority(Notification.PRIORITY_HIGH);

        if (notificationType.equals(NotificationType.REQUEST_RECEIVED) || notificationType.equals(NotificationType.REQUEST_CANCELED)) 
            actionOne = TaskStackBuilder.create(app)
                    .addNextIntent(intentActionOne)
                    .getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
            actionTwo = TaskStackBuilder.create(app)
                    .addNextIntent(intentActionTwo)
                    .getPendingIntent(1, PendingIntent.FLAG_ONE_SHOT);

            builder.addAction(icon_one, actionTextOne, actionOne)
                    .addAction(icon_two, actionTextTwo, actionTwo);
        

        Notification notification = builder.build();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
            int smallIconViewId = getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());

            if (smallIconViewId != 0) 
                if (notification.contentIntent != null)
                    notification.contentView.setViewVisibility(smallIconViewId, View.INVISIBLE);

                if (notification.headsUpContentView != null)
                    notification.headsUpContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);

                if (notification.bigContentView != null)
                    notification.bigContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
            
        
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify("MyApp", Long.valueOf(System.currentTimeMillis()).intValue(), notification);
    

当我收到 REQUEST_RECEIVED 的通知并且没有执行任何单击操作,然后如果收到 REQUEST_CANCELED 的下一个通知,在这种情况下,第二个通知操作单击执行第一个通知的单击操作,而第二个通知的操作集不起作用。

等待您的建议。

【问题讨论】:

【参考方案1】:

我不能 100% 确定它是否会起作用,但我会这样说:

intentActionOne.setAction("ACTION_ONE");
intentActionTwo.setAction("ACTION_TWO");

和/或

actionOne = TaskStackBuilder.create(app)
                    .addNextIntent(intentActionOne)
                    .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
actionTwo = TaskStackBuilder.create(app)
                    .addNextIntent(intentActionTwo)
                    .getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

@Raphul 我已经试过那个溶胶了。但是 FLAG_UPDATE_CURRENT 将删除以前的待处理意图并对其进行更新,因此它将无法正常工作。 你有没有在这些意图上尝试过这种与 setAction() 方法的组合? 好的,最后想到,因为我有一个应用程序同时包含许多通知并且有类似的问题。对于 NotificationType.REQUEST_RECEIVED 设置动作“ACTION_ONE”和“ACTION_TWO”,对于 NotificationType.REQUEST_CANCELED 设置“ACTION_THREE”和“ACTION_FOUR”

以上是关于Android:通知动作点击非统一行为的主要内容,如果未能解决你的问题,请参考以下文章

怎样做点击推送消息,跳转到指定页面

Android 使用通知

快速用户通知点击

动作按钮的 Swift UNNotificationRequest 标识符点击了本地通知

Linux非统一内存分发

Android 推送通知未收到