针对优先级消息的Android N内联回复问题

时间:2016-11-15 07:58:26

标签: android notifications whatsapp

我在对优先级消息进行内联回复时发现了一个问题。如果您在其他应用程序或主屏幕上显示通知时直接回复。回复工作正常,但不会从状态栏和通知面板中删除通知。

同样的问题也出现在WhatsApp上。

我已经完成Google Developer DocAndroid Developer Blog,但无法弄清楚决议。

以下是测试视频:

通知代码

 /**
 * Create chat message reply ui
 *
 * @param singleUserMessage
 * @param context
 * @param channelId
 * @param RecipientUserId
 * @param builder
 */
private void setDirectChatReply(boolean singleUserMessage, Context context, String channelId, String RecipientUserId, NotificationCompat.Builder builder) {
    if (singleUserMessage && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        // Key for the string that's delivered in the action's intent.
        RemoteInput remoteInput = new RemoteInput.Builder(Constants.GCM.DIRECT_REPLY_KEY)
                .setLabel(getString(R.string.direct_reply_notif_label))
                .build();
        // Create the reply action and add the remote input.
        Intent broadcastIntent = new Intent(context, ReplyBroadcastReceiver.class);
        broadcastIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        broadcastIntent.putExtra(Constants.Conversations.CHANNEL_ID, channelId);
        broadcastIntent.putExtra(Constants.Conversations.RECIPIENT_USER_ID, RecipientUserId);
        broadcastIntent.putExtra(Constants.Conversations.CHAT_USER_ID, mDesidimeSharedPreferences.getMyChatUserId());
        PendingIntent replyIntent = PendingIntent.getBroadcast(context, Constants.NotificationValues.CHAT_NOTIFICATION_ID,
                broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_send,
                getString(R.string.notif_action_reply), replyIntent).addRemoteInput(remoteInput).build();
        builder.addAction(action);
    }
}

DirectReply类来处理内联回复

    public class ReplyBroadcastReceiver extends BroadcastReceiver implements MessageSenderTask.ChatMessageProcessor {
    private static final String TAG = "ReplyBroadcastReceiver";
    private Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        // Hit Analytics
        DesidimeSharedPreferences sharedPreferences = new DesidimeSharedPreferences(context);
        Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
        if (remoteInput != null) {
            String reply = remoteInput.getString(Constants.GCM.DIRECT_REPLY_KEY);
            if (!TextUtils.isEmpty(reply)) {
                Bundle dataBundle = intent.getExtras();
                dataBundle.putString(Constants.Conversations.MESSAGE_KEY, reply);
                dataBundle.putString(Constants.Conversations.MESSAGE_TYPE, Constants.Conversations.MESSGE_TYPE_CHAT);
                MessageSenderTask messageSenderTask = new MessageSenderTask(context, this);
                messageSenderTask.setMessageData(dataBundle, sharedPreferences.getUserId());
                messageSenderTask.execute();
            }

        }
    }

    @Override
    public void onMessageProcessed(Bundle data) {
        if (data != null) {
            NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.cancel(Constants.NotificationValues.CHAT_NOTIFICATION_ID);
            updateUnreadConversationStatus(data.getString(Constants.Conversations.RECIPIENT_USER_ID));
        }


    }

    /**
     * Locally manage read/unread conversation for Recent user list
     * @param recipientId
     */
    private void updateUnreadConversationStatus(String recipientId) {
        try {
            if (StringUtils.isNotEmpty(recipientId)) {
                ChatManager chatManager = new ChatManager(DBTables.Conversations.TABLE_RECENT_USER_LIST, mContext);
                chatManager.updateConversationUnreadStatus(recipientId);
            }
        } catch (Exception e) {
            Crashlytics.log(Log.DEBUG, TAG, "updateUnreadConversationStatus recipientId : "+recipientId);
            Crashlytics.logException(e);
        }
    }

    @Override
    public void onMessageProcessedFailed() {
        Log.d(TAG, "onMessageProcessedFailed");
    }
}

在onMessageProcessed上,我将从通知面板中删除通知。

0 个答案:

没有答案