How to access an Activity by clicking on the notification without losing the data

Asked

Viewed 58 times

1

Hello, I would like to ask for help because I built an app with a stopwatch and after a long time managed to enable notifications for Android Oreo+, but I can’t access the stopwatch Activity through this notification and would also like if you can, help me open the timer Activity via notification without restarting it

follows below the code

Notificationhelper.java

public NotificationHelper(Context base) {
    super(base);
    createChannels();

}

private void createChannels() {
    NotificationChannel myChannel = new NotificationChannel(MY_CHANNEL_ID, MY_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
    myChannel.enableLights(true);


    getManager().createNotificationChannel(myChannel);
}

public NotificationManager getManager() {
    if (manager == null)
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    return manager;

}


public Notification.Builder getmyChannelNotification(String title, String body) {

    return new Notification.Builder(getApplicationContext(), MY_CHANNEL_ID)
            .setAutoCancel(false)
            .setContentText("Acesso rápido ao app")
            .setSmallIcon(R.drawable.timer_notification);
}

}

Chronometer.java

  private NotificationChannel NotificationChannel;
private TextView Title;
private TextView Content;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chronometer);

    helper = new NotificationHelper(this);
    Title = (TextView)findViewById(R.id.Title);
    Content = (TextView)findViewById(R.id.Content);
    notificationbtn = (Button)findViewById(R.id.notificationbtn);
    notificationbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String title = Title.getText().toString();
            String content = Content.getText().toString();
            Notification.Builder builder = helper.getmyChannelNotification(title, content );
            helper.getManager().notify(new Random().nextInt(),builder.build());
        }
    });

1 answer

1

Come on!

To open Activity from the notification it is necessary to inform this in Notification.Builder.

This can be done as follows:

 // Cria um Intent da Activity que você deseja abrir
Intent resultIntent = new Intent(this, ActivityQueVoceQuerAbrir.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Cria um PendingIntent a partir da intent criada acima
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

Now with the pending input in hand, you need to inform it in Notification.Builder. See:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(resultPendingIntent);

In the setContentIntent method is where we pass the created Pendingintent, informing so that we want this Intent to be triggered when the user clicks on the notification!

Now as to the problem of losing the timer count when closing the app, you will have to persist this data so that it is saved even if the user closes the app.

What you can do is look this topic here that teaches how to save data in Sqlite locally on Android. Once you know how to save the data locally, just save the time the timer started, and whenever you open Activity, read that time and calculate how much time has passed, and then start the timer from there!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.