Android:通知操作单击非统一行为

时间:2017-02-08 14:28:41

标签: android firebase push-notification notifications android-notifications

我在通知中遇到问题我正在使用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的下一个通知,那么第二个通知操作点击执行第一个通知的点击操作,而第二个通知的操作集不起作用。 / p> 等待你的建议。

1 个答案:

答案 0 :(得分:1)

我不是百分百确定它是否可行,但我会这样做:

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