Skip to content
Advertisement

Action button on Notification if not clicked on certain time will do something else

I have an app that if something happens it will pop up a Notification with an action button. if it pressed it will the app running but if not for a certain amount of time it will run another code. I’m still confused about how to make that

EDIT :

Here is The code I tried Main Activity.java :

public class MainActivity extends AppCompatActivity {
    public NotificationManagerCompat notificationManager;
    public TextView mViewLabel;
    public ArrayList<Integer> lst = new ArrayList<Integer>();
    boolean continueThread = true;
    int count =0;
    Thread t;
    Timer j = new java.util.Timer();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        notificationManager = NotificationManagerCompat.from(this);
        mViewLabel = (TextView) findViewById(R.id.textChanger);



        t = new Thread(){
            @Override
            public void run() {
                if (continueThread) {
                    while (continueThread) {
                        lst.add(70);
                        lst.add(71);
                        lst.add(72);
                        lst.add(73);
                        lst.add(74);
                        lst.add(75);
                        try {
                            Thread.sleep(1000);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Collections.shuffle(lst);
                                    mViewLabel.setText(String.valueOf(lst.get(count)));
                                }

                            });

                        }catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        count++;

                    }
                }

            }
        };


    }
    public void BtnStart(View view){
        t.start();
        j.schedule(
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                        while(continueThread){
                            if(lst.get(count) < 80){
                                sendOnChannel1();
                                break;
                            }
                            count++;
                        }
                    }
                },
                5000
        );
    }
    public void BtnStop(View view){
        if(continueThread){
            continueThread=false;
            mViewLabel.setText("0");
        }
    }
    public void BtnReset(View view){
        if(!continueThread){
           continueThread=true;
           mViewLabel.setText("Click Start To Simulate Heartbeat");
        }
    }

    public void sendOnChannel1() {
        String title = "Title";
        String message = "Testing";
        Intent activityIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this,
                0, activityIntent, 0);
        Intent broadcastIntent = new Intent(this, NotificationReceiver.class);
        broadcastIntent.putExtra("toastMessage", message);
        PendingIntent actionIntent = PendingIntent.getBroadcast(this,
                0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_one)
                .setContentTitle(title)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setColor(Color.BLUE)
                .setContentIntent(contentIntent)
                .addAction(R.mipmap.ic_launcher, "Toast", actionIntent)
                .build();
        notificationManager.notify(1, notification);
    }

if something happens it will pop up a Notification with an action button is on this code

j.schedule(
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                        while(continueThread){
                            if(lst.get(count) < 80){
                                sendOnChannel1();
                                break;
                            }
                            count++;
                        }
                    }
                },
                5000
        );

Advertisement

Answer

From your example. You could pass the current time into the intent.

So from your MainActivity.java

public void sendOnChannel1(View v) {
    String title = editTextTitle.getText().toString();
    String message = editTextMessage.getText().toString();
    Intent activityIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this,
            0, activityIntent, 0);
    Intent broadcastIntent = new Intent(this, NotificationReceiver.class);
    broadcastIntent.putExtra("toastMessage", message);
    broadcastIntent.putExtra("time", Calendar.getInstance().getTimeInMillis()); //**Add here
    PendingIntent actionIntent = PendingIntent.getBroadcast(this,
            0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
            .setSmallIcon(R.drawable.ic_one)
            .setContentTitle(title)
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setColor(Color.BLUE)
            .setContentIntent(contentIntent)
            .setAutoCancel(true)
            .setOnlyAlertOnce(true)
            .addAction(R.mipmap.ic_launcher, "Toast", actionIntent)
            .build();
    notificationManager.notify(1, notification);
}

Then when you receive it within the NotificationReceiver.java Compare the date time etc.

public class NotificationReceiver extends BroadcastReceiver {

Integer TEN_MINUETS = 1000 * 60 * 10;

@Override
public void onReceive(Context context, Intent intent) {
    String message = intent.getStringExtra("toastMessage");
    long time = intent.getLongExtra("time", -1);
    if(time == -1){
        Toast.makeText(context, "Example, no time found", Toast.LENGTH_SHORT).show();
    }
    if(time + TEN_MINUETS < Calendar.getInstance().getTimeInMillis()){
        //10 minutes passed do something else
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        return;
    }
    //10 miuntes not passed do something more?
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

}

}

Just make sure to check the intent has data etc.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement