3
The purpose of the notification is to show a question to the user, and the user has two response options, "YES" or "NO".
The problem is knowing which button the user pressed.
Another thing that I didn’t intend but happen, is that the user is redirected to an Activity, and I didn’t want that.
If he presses one of the buttons he is redirected to "Mainactivty", and what I only want from this action is for mainActivity to receive only any information about which button the user pressed, in order to make the necessary records. My intention is only that the user answer the question without being redirected to anything.
I’ll leave the piece of code where all this happens. I hope I have been as explicit as possible, and I would like to know if what I have described above is possible to do.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
....
int id = (int) System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_MONTH, 29);
calendar.set(Calendar.MONTH, 5);
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 38);
calendar.set(Calendar.SECOND, 00);
AlarmManager alarm = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
Intent newintent = new Intent(this, Notification_Create.class);
intent.putExtra("id", id);
PendingIntent pending = PendingIntent.getBroadcast(this, id, newintent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending);
}
}
public class Notification_Create extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int id = extras.getInt("id");
Bundle extras = intent.getExtras();
Intent intentTPC = new Intent(context, MainActivity.class);
intentTPC.putExtra("id", String.valueOf(id));
PendingIntent resultPendingIntent =
PendingIntent.getActivity(context,
0,
intentTPC,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle("Organização: ")
.setContentText("Tu hoje vais à natação?")
.setContentIntent(resultPendingIntent)
.setAutoCancel(true);
mBuilder.setSmallIcon(R.drawable.ic_button);
mBuilder.addAction(R.drawable.ic_button,"Sim",resultPendingIntent);
mBuilder.addAction(R.drawable.ic_button,"Não",resultPendingIntent);
mBuilder.setPriority(Notification.PRIORITY_MAX);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, mBuilder.build());
}
}
thanks, as soon as I can! @ramaral
– Sunl
Managed to implement?
– ramaral
I am testing the use of intentService that suggested me @ramaral
– Sunl
I put what you suggested to me in mainActivity, I took advantage of the Notification_create class, and I extended the Intentservice...It worked, the notification appeared...but I still have the problem, how do I add action the two buttons? I think I misinterpreted what you told me...?
– Sunl
You must create an Intentservice for each of the buttons. In it you must put the code that you want to be executed when the respective button is clicked.
– ramaral
I created a pending Intent with an Intentservice for each button as suggested. To test if it really worked I entered a log.cat with a message if the user pressed "yes" and nothing happens...the log.cat does not appear @ramaral
– Sunl
Declared Intentservice on Androidmanifest.xml?
– ramaral
yes declared <service android:name=".Notification.Registocancelaservice" /> <service android:name=".Notification.Registocriadoservice" /> @ramaral
– Sunl
I don’t know what else to say. If you correctly implement what is indicated in the answer, it has to work. It can only be something you are doing wrong.
– ramaral