1

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

Community
  • 1
  • 1
Calimbo
  • 81
  • 1
  • 3
  • 13
  • See this [Answer](http://stackoverflow.com/questions/15350998/determine-addaction-click-for-android-notifications?answertab=oldest#tab-top) – Harin Apr 13 '15 at 12:22

2 Answers2

1

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.

Rajan Bhavsar
  • 1,977
  • 11
  • 25
  • Sorry but I don't understand, but ... where is button ? ".addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)", my problem is similar to here https://udinic.wordpress.com/2012/07/24/adding-more-actions-to-jellybean-notifications/ – Calimbo Apr 13 '15 at 13:32
  • We should have to Checked out that api level and based on that we have to set the Notification Layout. – Rajan Bhavsar Apr 13 '15 at 13:58
  • Your idea is good, but how I can "Button = (LinearLayout)context.findViewById(R.id.Button);" ??? – Calimbo Apr 13 '15 at 14:41
  • we are able to access Button by Button btn = (Button)contentView.findViewById(R.id.button); and now we also used btn.setOnClickListner(); – Rajan Bhavsar Apr 14 '15 at 04:45
  • Wait wait wait ... "RemoteViews contentView " you know about remoteviews ? .... http://developer.android.com/reference/android/widget/RemoteViews.html – Calimbo Apr 14 '15 at 15:18
  • You can achieve the on Click of button in Notification Layout with following code as : contentView.setOnClickPendingIntent(R.id.button, notificationIntent); – Rajan Bhavsar Apr 15 '15 at 04:32
  • How I can "contentView.setOnClickPendingIntent" assing with button ? button = (Button)contentView.setOnClickPendingIntent.. and manage the event of the button itself ? – Calimbo Apr 15 '15 at 20:51
0
//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);
}
Anthone
  • 2,156
  • 21
  • 26