Reputation: 81
I try action in my Notification click a button from "builder", but I can't.
I read documentation oficial :
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Notification notification = new Notification.Builder(context)
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat_player)
// Add media control buttons that invoke intents in your media service
.addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0 HERE A BUTTON
.addAction(R.drawable.ic_pause, "Pause", pausePendingIntent) // #1 HERE A BUTTON
.addAction(R.drawable.ic_next, "Next", nextPendingIntent) // #2 HERE A BUTTON
// Apply the media style template
.setStyle(new Notification.MediaStyle()
.setShowActionsInCompactView(1 /* #1: pause button */)
.setMediaSession(mMediaSession.getSessionToken())
.setContentTitle("Wonderful music")
.setContentText("My Awesome Band")
.setLargeIcon(albumArtBitmap)
.build();
I see this post :
Android, get Notifications Responses
And someone say :
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Notification n = sbn.getNotification();
for(Action action : n.actions){
String title = action.title.toString;
...
}
}
But I can't use because I not extends of "NotificationListenerService" I extends of IntentService
How I can click Buttons and after occurs a event ?
Any can know differences about NotificationListenerService and IntentService ?
I hope explain correctly
Upvotes: 1
Views: 3941
Reputation: 2290
//Exemple of notification with Button
private static void scheduleNotificationWithButton(Context context, String message) {
int notifReCode = 1;
//What happen when you will click on button
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);
//Button
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Go", pendingIntent).build();
//Notification
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Back to Application ?")
.setContentTitle("Amazing news")
.addAction(action) //add buton
.build();
//Send notification
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
Upvotes: 0
Reputation: 1867
i was created the my Custom notification by Custom Message and Drawable Image to Notify and also added the handler while user was clicked on Notification to open the New Screen.
private void generateNotification(Context context, String message,
String pid, String type) {
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context,
AddFriendRequestActivity.class);
notificationIntent.putExtra("screen", "noti");
notificationIntent.putExtra("message", message);
notificationIntent.putExtra("friendId", pid);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification notification = new NotificationCompat.Builder(this)
.setContentIntent(intent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MeetAtAirport").setContentText(message).build();
notification.setLatestEventInfo(context, title, message, intent);
RemoteViews contentView = new RemoteViews(this.getPackageName(),
R.layout.custom_notification);// set your custom layout
contentView
.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, "MeetAtAirport");
contentView.setTextViewText(R.id.text, message);
notification.bigContentView = contentView;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final int noteId = 1232;
notificationManager.notify(noteId, notification);
}
There is one Notification Layout as Follows with name as R.layout.custom_notification:
`
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
android:src="@drawable/icon" />
<TextView
android:id="@+id/title"
style="@style/txtHelvBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:textSize="20dp" />
<TextView
android:id="@+id/text"
style="@style/txtHelvBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_toRightOf="@id/image" />
`
In this Layout File You are able to add your Buttons and handle the Button's Click as Other are used in above code.
Upvotes: 1