Mr. 0x50
Mr. 0x50

Reputation: 314

Android - Push Notification - Answer

A lot of messenger/apps send Android Notification where I can reply directly in the Notification without opening the App. This feature is in WhatsApp or Telegram for example.

My question now is, how to do this?

// Update:

Here is the code i tried:

// 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());

I have the following 2 Problems: getting this error

and

and this error

// UPDATE 2:

After fixing my first problem, i got a new Problem:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

But this could be fixed by adding Looper.prepare();

Upvotes: 2

Views: 1316

Answers (1)

Budius
Budius

Reputation: 39856

there're two ways:

  1. That's how Hangtous do it: using the reply API introduced in Nougat. Just follow the official guidelines: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#direct

  2. As far as I'm aware that's how WhatsApp do it (I'm not sure telegram): Create an Activity with a transparent background (set in the theme) and some content on the top of the screen with input field and send button.

update:

now that you showed some code, it's actually pretty simply. You're mixing native API and Compat API.

  • Notification is native from android in the package android.app.Notification
  • NotificationCompat is from the compat support library in the package android.support.v4.app.NotificationCompat

You can't mix them. Pick one and use just that one. I suggest use the compat for everything because it's auto handles backward compatibility.

Upvotes: 2

Related Questions