Вам необходимо создать службу для обнаружения события Click: например, создать NotificationIntentService.class
и указать код:
public class NotificationIntentService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public NotificationIntentService() {
super("notificationIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
switch (intent.getAction()) {
case "left":
android.os.Handler leftHandler = new android.os.Handler(Looper.getMainLooper());
leftHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getBaseContext(),
"You clicked the left button", Toast.LENGTH_LONG).show();
}
});
break;
case "right":
android.os.Handler rightHandler = new android.os.Handler(Looper.getMainLooper());
rightHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getBaseContext(), "You clicked the right button", Toast.LENGTH_LONG).show();
}
});
break;
}
}
}
Добавьте этот метод к вашей деятельности:
private void sendNotification() {
RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.view_expanded_notification);
expandedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
expandedView.setTextViewText(R.id.notification_message, mEditText.getText());
// adding action to left button
Intent leftIntent = new Intent(this, NotificationIntentService.class);
leftIntent.setAction("left");
expandedView.setOnClickPendingIntent(R.id.left_button, PendingIntent.getService(this, 0, leftIntent, PendingIntent.FLAG_UPDATE_CURRENT));
// adding action to right button
Intent rightIntent = new Intent(this, NotificationIntentService.class);
rightIntent.setAction("right");
expandedView.setOnClickPendingIntent(R.id.right_button, PendingIntent.getService(this, 1, rightIntent, PendingIntent.FLAG_UPDATE_CURRENT));
RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.view_collapsed_notification);
collapsedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// these are the three things a NotificationCompat.Builder object requires at a minimum
.setSmallIcon(R.drawable.ic_pawprint)
.setContentTitle(NOTIFICATION_TITLE)
.setContentText(CONTENT_TEXT)
// notification will be dismissed when tapped
.setAutoCancel(true)
// tapping notification will open MainActivity
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
// setting the custom collapsed and expanded views
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
// setting style to DecoratedCustomViewStyle() is necessary for custom views to display
.setStyle(new android.support.v7.app.NotificationCompat.DecoratedCustomViewStyle());
// retrieves android.app.NotificationManager
NotificationManager notificationManager = (android.app.NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}