【问题标题】:How to make Android Direct Reply Notification feature work in pre - Android N devices?如何使 Android 直接回复通知功能在 Android N 之前的设备中工作?
【发布时间】:2016-06-16 05:33:46
【问题描述】:

我正在尝试在我的应用中实现 Android 直接回复通知。 我已经成功地在 Android N 模拟器中实现了它。但它不适用于 Marshmallow 设备。当我单击通知中的操作按钮时,回复编辑文本未显示在 Android N 以下的设备中。我知道此功能将在 Android N 之前的设备中工作,因为它在 WhatsApp 中可用。

我的问题是如何让它在 Android N 之前的设备上运行?我在这里分享我的代码。任何帮助都会很棒。谢谢。

主活动

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.RemoteInput;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

// Key for the string that's delivered in the action's intent.
public static final String KEY_TEXT_REPLY = "key_text_reply";
private static final int notificationID = 1234;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);




    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        startNotif();
        }
    });
}

private void startNotif() {

    Intent intent = new Intent(this, NotificationReciever.class);
// use System.currentTimeMillis() to have a unique ID for the pending intent
    final PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    String replyLabel = "Reply";
    RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
            .setLabel(replyLabel)
            .build();

    // Create the reply action and add the remote input.
    Notification.Action action =
            new Notification.Action.Builder(R.drawable.ic_send,
                    "Action", pIntent)
                    .addRemoteInput(remoteInput)
                    .build();

    // Build the notification and add the action.
    Notification newMessageNotification =
            new Notification.Builder(MainActivity.this)
                    .setSmallIcon(R.drawable.ic_remove_circle_black_48dp)
                    .setContentTitle("Title")
                    .setContentText("Content")
                    .addAction(action).build();

// Issue the notification.
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(notificationID, newMessageNotification);
    }
}

通知接收者

import android.app.Activity;
import android.app.RemoteInput;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

/**
 * Created by User on 13-Jun-16.
 */
public class NotificationReciever extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Toast.makeText(this,getMessageText(getIntent()),Toast.LENGTH_SHORT).show();
    }

    private CharSequence getMessageText(Intent intent) {
        Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
        if (remoteInput != null) {
            return remoteInput.getCharSequence(MainActivity.KEY_TEXT_REPLY);
        }
        Toast.makeText(this,"Remoteinput is null",Toast.LENGTH_SHORT).show();

        return null;
    }
}

【问题讨论】:

  • 你找到答案了吗?
  • @Prakash 我已经尝试了所有方法,但直接回复在 Noughat 以下的版本中不起作用。因此,正如 TommyChan 在下面的回答中所说,我们将不得不启动一个活动,通过直接回复(如其中的布局)来执行我们想要的操作。我认为这就是它在whatsapp中的完成方式。如果我错了纠正我。 :)
  • @SudheeshMohan 我正在尝试实现同样的事情。您能否介绍一下您是如何解决这个问题的?您找到的最佳解决方案是什么?
  • N之前,可以通过隐藏在支持AndroidWear回复的应用通知中的AndroidWear组件来实现

标签: android android-notifications backwards-compatibility


【解决方案1】:

在文档中,它告诉您不要使用广播接收器或低于 N 的 api 服务。因此,您必须启动一个执行您想要的操作并将其包装在待处理意图中的活动。

if (isDirectReplySupported) {
  return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
}
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

  • 那么 Whatsapp(N 以下)实际上是在启动一个回复活动而不是直接回复?
  • 很有可能。在活动中,他们将拥有解析意图的代码,如果存在诸如直接回复之类的标志,则会发送一条消息。这是 N 之前穿戴式应用的模式。
【解决方案2】:

实现这个功能很简单,首先制作一个广播接收器,像往常一样创建一个动作按钮并与pendingintent挂钩。

然后将该意图连接到推送通知对象并通知。

在广播接收器中处理阅读文本部分并更新到通知。

请参阅此帖子以获取示例示例http://www.feelzdroid.com/2017/01/android-n-inline-reply-push-notifications.html

【讨论】:

  • 您可能需要再次检查该问题,因为它要求建议一种在 Android N 之前的设备中实现 RemoteInput 的方法,而不是针对 Android N 设备。
猜你喜欢
  • 2017-03-15
  • 1970-01-01
  • 2020-01-09
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
相关资源
最近更新 更多