Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Android Direct Reply Notification feature work in pre - Android N devices?

I am trying to implement Android Direct Reply notification in my app. I've managed to implement it successfully in Android N emulator. But it is not working on Marshmallow devices. When I click on action button in notification the reply edittext is not showing in devices below Android N. I know this feature will work in pre Android N devices, as it is available in WhatsApp.

My question is how to make it work in pre Android N devices? I'm sharing my code here. Any help would be great. Thanks.

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;
    }
}
like image 654
Sudheesh Mohan Avatar asked Jun 16 '16 05:06

Sudheesh Mohan


People also ask

What is remote notification in Android?

Remote Notifications are notifications sent to a mobile device using a data-channel from a service provider in real-time.


1 Answers

In the docs it tells you to not use a broadcast receiver or a service for api below N. So you'd have to launch an activity which does what you want and wrap it in a pending intent instead.

if (isDirectReplySupported) {
  return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
}
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
like image 51
Tommy Chan Avatar answered Dec 17 '22 05:12

Tommy Chan