Android NotificationCompat.Action.Builder从数组动态创建多个动作

时间:2017-04-20 15:35:02

标签: android push-notification

我有一个基于动作类型创建动作所需的动作列表。我目前正在设置它的方式会覆盖之前的方式。我该如何解决这个问题?

让我们使用:

       "actions": [{
            "type":"response", 
            "text": "Confirm"
        }, {
            "type":"response", 
            "text": "Decline"
        }, {
            "type":"info", 
            "text": "More Info",
            "link": "http://www.notifyd.com"
        }]

然后:

        for (int i=0; i < actions.length(); i++) {
            JSONObject action = actions.getJSONObject(i);
            String type = (String)action.get("type");
            if (type.equals("response")) {
                Intent actionIntent = new Intent("blah blah blah");
                String text = (String)action.get("text");
                actionIntent.putExtra("text", text);
                PendingIntent pendingAction = PendingIntent.getBroadcast(this, id, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                NotificationCompat.Action actionItem = new NotificationCompat.Action.Builder(0, text, pendingAction).build();
                notificationBuilder.addAction(actionItem);
            } else if (type.equals("info")) {
                // logic here is fine
            }
        }

然后问题变成: 无论响应如何(在通知中选择了类型&#39;响应&#39;的行为),&#34;拒绝&#34;被退回......

任何方向,欢呼,嘲笑,腐烂的水果等都值得赞赏!

1 个答案:

答案 0 :(得分:1)

您可能针对不同的requestCode使用相同的PendingIntent,针对不同的Id使用相同的通知Notification

id的通知requestCodePendingIntent应为unique,以获得正确的通知data

// Pending Intent
PendingIntent pendingAction = PendingIntent.getBroadcast(this, UNIQUE_REQUEST_CODE, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);


// Notification Manager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(UNIQUE_NOTIFICATION_ID, notificationBuilder.build());

希望这会有所帮助〜