0

So i have an alarm app...and when the receiver gets an intent from an alarm class, it creates a notification and builds it..but i just cant seem to figure out how to add onclick event to that button..i want it to implement a function not to just get an intent

this is my receiver

 public class AlarmReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context arg0, Intent arg1) {
    Context context= arg0;
    Intent intent = new Intent(context,MainActivity.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,0);

    NotificationCompat.Builder mBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.mini)
                    .setContentTitle(context.getResources().getString(R.string.message_box_title))
                    .setContentText(context.getResources().getString(R.string.message_timesheet_not_up_to_date))
                    .addAction(R.drawable.bell,"snooze",pendingIntent);
    Intent resultIntent = new Intent(context, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());







    Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();

    Integer get_your_alarm_choice = arg1.getExtras().getInt("alarm_choice");
    Log.e("alarm choice is",get_your_alarm_choice.toString());
}

any help would be really appreciated

spyder3anz
  • 27
  • 8
  • add resultIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); let me know if its work or not – Solaiman Hossain May 25 '17 at 16:08
  • but where should i add the code that i want the button to do? @msh,nayan – spyder3anz May 25 '17 at 16:19
  • Intent intent = new Intent(context,MainActivity.class); after this line add intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); later You write Intent resultIntent = new Intent(context, MainActivity.class); after this line add resultIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); – Solaiman Hossain May 25 '17 at 16:21
  • i already did that...what i am asking is how to click the button on the notification to trigger a function @msh.nayan – spyder3anz May 25 '17 at 16:30
  • can't understand your question mate. do u want to perform a function when notification is clicked rather than going to your Main Activity? – Solaiman Hossain May 25 '17 at 16:36
  • my notification already takes me to MainActivity...there is abutton in my notification and i want when i click it to implement a function..i dont know how to add on click listener to that button..thats all im asking @msh.nayan – spyder3anz May 25 '17 at 16:38
  • kindly look at this https://stackoverflow.com/questions/13822509/android-call-method-on-notification-click – Solaiman Hossain May 25 '17 at 16:53
  • that has nothing to do with my question,... adding a button to the notification is not like clicking on the notification...thank you anyway for trying @msh,nayan – spyder3anz May 25 '17 at 17:09

1 Answers1

0

I think you want to add a custom button in your notification and want to click it.

Please try below code if you need the same:

You have to use RemoteViews for this.

I have created one custom layout named notification_normal_view.xml.

In my notification_normal_view , I have one TextView i.e.txtSnooze and on click I want to open SnoozeActivity and if I click at any other part of notification I want to open MainActivity.

So in your receiver :

// Using RemoteViews to bind custom layouts into Notification
RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.notification_normal_view);

Intent snoozeIntent = new Intent(context, SnoozeActivity.class);
snoozeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK | Notification.FLAG_AUTO_CANCEL);

PendingIntent pSnoozeIntent = PendingIntent.getBroadcast(context,NOTIFICATION_ID,snoozeIntent,PendingIntent.FLAG_UPDATE_CURRENT);

Intent intent = new Intent(context, ExoVideoPlayer.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK | Notification.FLAG_AUTO_CANCEL);
PendingIntent pIntent = PendingIntent.getActivity(context, NOTIFICATION_ID,intent, PendingIntent.FLAG_UPDATE_CURRENT);

notificationView.setOnClickPendingIntent(R.id.txt_snooze, pSnoozeIntent);

Notification notificationBuilder = new Notification.Builder(context)
                .setSound(soundUri)
                .setSmallIcon(icon)
                .setAutoCancel(true)
                .build();

//set your view to notification
notificationBuilder.contentView = notificationView;
notificationBuilder.flags = Notification.FLAG_AUTO_CANCEL;
notificationBuilder.icon = R.mipmap.ic_launcher;
notificationBuilder.contentIntent = pIntent;

NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(NOTIFICATION_ID, notificationBuilder);
Patrick R
  • 6,621
  • 1
  • 24
  • 27
  • that you for that it was very helpfull... one last thing..do you know how i can have that notification button to snooze an alarm? your code lets me send a pending intent when using the button...i want to be able to click it and have it do something like a function...in my case snooze the alarm @PatrickR – spyder3anz May 27 '17 at 11:18