Permalink
Browse files

Add DirectReply and ReplyReceiver

  • Loading branch information...
adamjodlowski committed Jun 21, 2016
1 parent 8162852 commit 20bd63351628047960396e2323039ce5f86e4958
@@ -15,7 +15,12 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DetailsActivity"></activity>
<activity android:name=".DetailsActivity" />
<receiver
android:name=".ReplyReceiver"
android:enabled="true"
android:exported="false" />
</application>
</manifest>
@@ -1,10 +1,13 @@
package io.github.adamjodlowski.notifications;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.RemoteInput;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
@@ -13,6 +16,7 @@
private Button button;
public static int NOTIFICATION_ID = 1;
public static final String KEY_NOTIFICATION_REPLY = "KEY_NOTIFICATION_REPLY";
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -37,6 +41,29 @@ public void onClick(View view) {
PendingIntent.FLAG_UPDATE_CURRENT
);
// Define PendingIntent for Reply action
PendingIntent replyPendingIntent = null;
// Call Activity on platforms that don't support DirectReply natively
if (Build.VERSION.SDK_INT < 24) {
replyPendingIntent = detailsPendingIntent;
} else { // Call BroadcastReceiver on platforms supporting DirectReply
replyPendingIntent = PendingIntent.getBroadcast(
MainActivity.this,
0,
new Intent(MainActivity.this, ReplyReceiver.class),
PendingIntent.FLAG_UPDATE_CURRENT
);
}
// Create RemoteInput and attach it to Notification Action
RemoteInput remoteInput = new RemoteInput.Builder(KEY_NOTIFICATION_REPLY)
.setLabel("Reply")
.build();
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
android.R.drawable.ic_menu_save, "Provide ID", replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
// NotificationCompat Builder takes care of backwards compatibility and
// provides clean API to create rich notifications
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this)
@@ -45,6 +72,7 @@ public void onClick(View view) {
.setContentText("See the details")
.setAutoCancel(true)
.setContentIntent(detailsPendingIntent)
.addAction(replyAction)
.addAction(android.R.drawable.ic_menu_compass, "Details", detailsPendingIntent)
.addAction(android.R.drawable.ic_menu_directions, "Show Map", detailsPendingIntent);
@@ -0,0 +1,33 @@
package io.github.adamjodlowski.notifications;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.RemoteInput;
import static android.content.Context.NOTIFICATION_SERVICE;
import static io.github.adamjodlowski.notifications.MainActivity.NOTIFICATION_ID;
public class ReplyReceiver extends BroadcastReceiver {
public ReplyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
CharSequence id = remoteInput.getCharSequence(MainActivity.KEY_NOTIFICATION_REPLY);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("Request received for ID: " + id);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
}

0 comments on commit 20bd633

Please sign in to comment.