0

I am using alarm manager to send a notification at particular time using this code:-

 Intent intent = new Intent(RemindersList.this, NotifierAlarm.class);
 intent.putExtra("Message",message);
 PendingIntent intent1 = PendingIntent.getBroadcast(RemindersList.this, reminders.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
 alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), intent1);

 Toast.makeText(RemindersList.this, "Inserted Successfully", Toast.LENGTH_SHORT).show();

In manifest receiver is :- <receiver android:name="com.example.package.NotifierAlarm" />

and the NotifierAlarm class is :-

public class NotifierAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {


 Uri alarmsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

    Intent intent1 = new Intent(context, MainActivity.class);
    intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addParentStack(MainActivity.class);
    taskStackBuilder.addNextIntent(intent1);

    PendingIntent intent2 = taskStackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);
    

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "9");

    NotificationChannel channel = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        channel = new NotificationChannel("channel_01", "ch", NotificationManager.IMPORTANCE_HIGH);
    }

  
    Notification notification =
             builder.setContentTitle("New Notification")
            .setContentText("msg")
            .setAutoCancel(true)
            .setSound(alarmsound)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentIntent(intent2)
            .setChannelId("my_channel_01")
            .setVisibility(VISIBILITY_PRIVATE)
            .build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        notificationManager.createNotificationChannel(channel);
    }
    notificationManager.notify(1, notification);

Here I want to add a button in notification with associative on click so that when user click on that button then some specific code should run without opening the app and then notification disappears that is i want something like Done/completed button of event

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
PRANAV SINGH
  • 1,000
  • 11
  • 17
  • [official docs](https://developer.android.com/training/notify-user/build-notification#Actions) are indeed a good place to start @PRANAV SINGH – Sekiro Jan 22 '21 at 06:18
  • @Sekiro I already have a look on official docs they were suggesting using broadcast receiver but since I have one therefore that method was not work for me – PRANAV SINGH Jan 22 '21 at 06:54
  • 1
    you can have any number of broadcast receiver, just make a new one and add the code to run in background. – Sekiro Jan 22 '21 at 07:14
  • 1
    android studio is just an IDE, it just helps you to write your code, so unless you're asking about a feature of the IDE specifically, there's no need to include the tag – a_local_nobody Jan 22 '21 at 07:37
  • 1
    I think [this](https://stackoverflow.com/a/56200802/10827064) will answer your question – oemel09 Jan 22 '21 at 08:47
  • @oemel09 thanks your answer perfectly for me but that had some missing part which is edited by me kindly check and give approval – PRANAV SINGH Jan 22 '21 at 14:42
  • [this](https://stackoverflow.com/a/56200802/13694485) answer is perfectly work for me – PRANAV SINGH Jan 22 '21 at 14:44

1 Answers1

0

I think you can remove PendingIntent in your NotificationComat, then the app will not navigation to MainActivity anymore.

And also, it is encourage for user to swipe left/right to dismiss the notification like any other Android user does.

Teo
  • 876
  • 9
  • 22
  • I want something like dismiss button but doesn't want dismiss button actually I want to do some background work when user click on that button otherwise notification will work as ordinary notification – PRANAV SINGH Jan 22 '21 at 07:02
  • Can try if remove ```PendingIntent```? but question is, if you didnt start your app, how can you run background task? – Teo Jan 22 '21 at 07:27
  • using broadcast receiver...for more information you can read the official docs whose link is commented by sekiro – PRANAV SINGH Jan 22 '21 at 07:33