Android - 推送通知 - 回答
Android - Push Notification - Answer
很多messenger/apps发送Android通知,不用打开App直接在通知里回复
例如,此功能在 WhatsApp 或 Telegram 中。
我现在的问题是,如何做到这一点?
//更新:
这是我试过的代码:
// Start Direct
// Key for the string that's delivered in the action's
final String KEY_TEXT_REPLY = "ring_direct_reply";
replyLabel = App.getInstance().getResources().getString(R.string.action_reply);
RemoteInput remoteInput = new RemoteInput.Builder(
.setLabel(replyLabel)
.build();
PendingIntent chatPendingIntent = PendingIntent.getBroadcast(
context,
0,
new Intent(context, NotificationReceiver.class),
PendingIntent.FLAG_UPDATE_CURRENT
);
// Create the reply action and add the remote input.
Notification.Action action =
new Notification.Action.Builder(
android.R.drawable.ic_delete,
App.getInstance().getResources().getString(R.string.action_reply),
chatPendingIntent
)
.addRemoteInput(remoteInput)
.build();
// End Direct Reply
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_action_push_notification)
.setContentTitle(title)
.setContentText(message)
.addAction(action)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
Intent resultIntent = new Intent(context, DialogsActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(DialogsActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
mBuilder.setAutoCancel(true);
mNotificationManager.notify(0, mBuilder.build());
我有以下两个问题:
和
// 更新 2:
解决我的第一个问题后,我遇到了一个新问题:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
但这可以通过添加 Looper.prepare();
来解决
有两种方法:
航图就是这样做的:使用牛轧糖中引入的回复API。只需遵循官方指南:https://developer.android.com/guide/topics/ui/notifiers/notifications.html#direct
据我所知,WhatsApp 是这样做的(我不确定电报):创建一个 Activity 具有透明背景(在主题中设置)和一些屏幕顶部的内容,带有输入字段和发送按钮。
更新:
现在您展示了一些代码,实际上非常简单。
您正在混合原生 API 和兼容 API.
Notification
原生于 android 包 android.app.Notification
NotificationCompat
来自软件包 android.support.v4.app.NotificationCompat
中的兼容支持库
你不能把它们混在一起。选择一个并只使用那个。我建议对所有内容都使用 compat,因为它会自动处理向后兼容性。
很多messenger/apps发送Android通知,不用打开App直接在通知里回复 例如,此功能在 WhatsApp 或 Telegram 中。
我现在的问题是,如何做到这一点?
//更新:
这是我试过的代码:
// Start Direct
// Key for the string that's delivered in the action's
final String KEY_TEXT_REPLY = "ring_direct_reply";
replyLabel = App.getInstance().getResources().getString(R.string.action_reply);
RemoteInput remoteInput = new RemoteInput.Builder(
.setLabel(replyLabel)
.build();
PendingIntent chatPendingIntent = PendingIntent.getBroadcast(
context,
0,
new Intent(context, NotificationReceiver.class),
PendingIntent.FLAG_UPDATE_CURRENT
);
// Create the reply action and add the remote input.
Notification.Action action =
new Notification.Action.Builder(
android.R.drawable.ic_delete,
App.getInstance().getResources().getString(R.string.action_reply),
chatPendingIntent
)
.addRemoteInput(remoteInput)
.build();
// End Direct Reply
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_action_push_notification)
.setContentTitle(title)
.setContentText(message)
.addAction(action)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
Intent resultIntent = new Intent(context, DialogsActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(DialogsActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
mBuilder.setAutoCancel(true);
mNotificationManager.notify(0, mBuilder.build());
我有以下两个问题:
和
// 更新 2:
解决我的第一个问题后,我遇到了一个新问题:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
但这可以通过添加 Looper.prepare();
来解决有两种方法:
航图就是这样做的:使用牛轧糖中引入的回复API。只需遵循官方指南:https://developer.android.com/guide/topics/ui/notifiers/notifications.html#direct
据我所知,WhatsApp 是这样做的(我不确定电报):创建一个 Activity 具有透明背景(在主题中设置)和一些屏幕顶部的内容,带有输入字段和发送按钮。
更新:
现在您展示了一些代码,实际上非常简单。 您正在混合原生 API 和兼容 API.
Notification
原生于 android 包android.app.Notification
NotificationCompat
来自软件包android.support.v4.app.NotificationCompat
中的兼容支持库
你不能把它们混在一起。选择一个并只使用那个。我建议对所有内容都使用 compat,因为它会自动处理向后兼容性。