Код: Выделить всё
private void showNotification(Context context) {
Person user = new Person.Builder()
.setName("Reminder")
.build();
NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(user)
.setConversationTitle("Reminder!")
.addMessage("Do you want to continue??", System.currentTimeMillis(), user);
// Yes Action (Reply)
Intent yesIntent = new Intent(context, MessagingService.class);
yesIntent.setAction(MessagingService.ACTION_REPLY);
PendingIntent yesPendingIntent = PendingIntent.getService(
context,
0,
yesIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE
);
NotificationCompat.Action yesAction =
new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_send, "Yes", yesPendingIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
.build();
// No Action (Mark as Read)
Intent noIntent = new Intent(context, MessagingService.class);
noIntent.setAction(MessagingService.ACTION_MARK_AS_READ);
PendingIntent noPendingIntent = PendingIntent.getService(
context,
1,
noIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
NotificationCompat.Action noAction =
new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_close_clear_cancel, "No", noPendingIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
.build();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("Reminder")
.setContentText("Do you want to continue?")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setStyle(messagingStyle)
.addAction(yesAction)
.addAction(noAction)
.extend(new NotificationCompat.CarExtender());
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
public class MessagingService extends Service {
public static final String ACTION_REPLY = "com.example.REPLY";
public static final String ACTION_MARK_AS_READ = "com.example.MARK_AS_READ";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleIntent(intent);
return START_NOT_STICKY;
}
private void handleIntent(Intent intent) {
if (intent == null) return;
String action = intent.getAction();
if (ACTION_REPLY.equals(action)) {
handleReplyAction();
} else if (ACTION_MARK_AS_READ.equals(action)) {
handleMarkAsReadAction();
}
stopSelf();
}
private void handleReplyAction() {
// Automatically set the reply to "Yes"
String replyMessage = "Yes";
Log.d("MessagingService", "Auto reply sent: " + replyMessage);
// Send the "Yes" response back to MainActivity
Intent mainActivityIntent = new Intent(this, com.example.notificationapp.MainActivity.class);
mainActivityIntent.putExtra("response", replyMessage);
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainActivityIntent);
// Cancel the notification
cancelNotification();
}
private void handleMarkAsReadAction() {
Log.d("MessagingService", "Notification marked as read");
cancelNotification();
}
private void cancelNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(com.example.notificationapp.MainActivity.NOTIFICATION_ID);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}}
< /code>
Я реализовал вышеперечисленное. Я хочу, чтобы уведомление, сгенерированное здесь, также отправилось в Android Auto Simulator (DHU). Я бы очень признателен за некоторую помощь. Если вам нужны другие фрагменты кода, пожалуйста, дайте мне знать. Заранее спасибо!
Подробнее здесь: https://stackoverflow.com/questions/795 ... rom-my-app
Мобильная версия