Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

android - On click on button of notification without opening app

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

question from:https://stackoverflow.com/questions/65840139/on-click-on-button-of-notification-without-opening-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...