D
Deleted User
Guest
Is it posible to make an action button that will cause notification to reappear after one hour. Then I want to make another action button that will cancel notification and make it reappear the next day again. Any ideas for how can I do this? Here is the code -> Main Activity:
- EditText write;
- Button notification;
- @override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- notification = findViewById(R.id.notification);
- write = findViewById(R.id.write);
- notification.setOnClickListener(new View.OnClickListener() {
- @override
- public void onClick(View v) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis(System.currentTimeMillis());
- calendar.set(Calendar.HOUR_OF_DAY, 12);
- calendar.set(Calendar.MINUTE, 57);
- calendar.set(Calendar.SECOND, 0);
- Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);
- intent.putExtra("TEXT", String.valueOf(write.getText()).trim());
- PendingIntent pendingIntent =PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
- alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
- }
- });
- }
- public class Notification_receiver extends BroadcastReceiver {
- @override
- public void onReceive(Context context, Intent intent) {
- String text = intent.getStringExtra("TEXT");
- NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
- Intent repeating_intent = new Intent(context, MainActivity.class);
- repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- repeating_intent.putExtra("action","snooze");
- repeating_intent.putExtra("action","cancel");
- PendingIntent pendingIntent =PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
- Intent snoozeintent = new Intent(context, ActionBtnReceiver.class);
- snoozeintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- snoozeintent.putExtra("action","snooze");
- PendingIntent snzpendingIntent = PendingIntent.getActivity(context, 100, snoozeintent, PendingIntent.FLAG_UPDATE_CURRENT);
- Intent cancelintent = new Intent(context, ActionCancelBtnReceiver.class);
- cancelintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- cancelintent.putExtra("action","cancel");
- PendingIntent cnlpendingIntent =PendingIntent.getActivity(context, 100, cancelintent, PendingIntent.FLAG_UPDATE_CURRENT);
- NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
- .setContentIntent(pendingIntent)
- .setSmallIcon(R.drawable.ic_launcher_background)
- .setContentTitle("Notification")
- .setContentText(text)
- .setAutoCancel(true)
- .addAction(R.drawable.ic_snooze_black_24dp, "Snooze", snzpendingIntent)
- .addAction(R.drawable.ic_cancel_black_24dp, "Cancel", cnlpendingIntent);
- notificationManager.notify(100, builder.build());
- }