我正在尝试在我的应用中实施Android直接回复通知。 我已成功在Android N模拟器中成功实施。但它不适用于Marshmallow设备。当我点击通知中的操作按钮时,回复edittext没有显示在Android N以下的设备中。我知道这个功能可以在Android N之前的设备中使用,因为它可以在WhatsApp中使用。
我的问题是如何让它在Android N之前的设备中运行?我在这里分享我的代码。任何帮助都会很棒。感谢。
MainActivity
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);
}
}
NotificationReciever
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;
}
}
答案 0 :(得分:2)
在文档中,它告诉您不要使用广播接收器或服务用于低于N的api。因此,您必须启动一个符合您想要的活动并将其包装在待处理的意图中。
if (isDirectReplySupported) {
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
}
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
答案 1 :(得分:-2)
实现此功能非常简单,首先制作一个广播接收器,像往常一样创建一个动作按钮&与pendingintent联系。
然后将该意图连接到推送通知对象&通知。
在broadcastreceiver处理阅读文本部分&更新为通知。
请参阅此帖子以获取示例http://www.feelzdroid.com/2017/01/android-n-inline-reply-push-notifications.html