Show a toast message only the first time a user enters the page

I have toast working on a page using ionViewDidEnter

ionViewDidEnter() {
let toast = this.toaster.create({
message: ‘message.’,
showCloseButton: true,
duration: 4000,
cssClass: ‘success’,
position: ‘bottom’,
closeButtonText: ‘Close’,
});
toast.present();
}

Is there a way to only have this appear the first time a user goes to that page, and not every time. Apologies if it’s an obvious question!

It depends how you store your user, are you using Firebase ?

What you could do is to create a boolean wich just asks if the user ever enterd the site or not.

As example:

ionViewDidEnter() {
  //HERE you are accessing the User itself *Note: The code may differ from your providers!!!*
    let profile = this.authProvider.getUserProfile();

  //HERE we ask if the boolean exists or not
    if (!profile.didEnter) {
    //This executes if the boolean is NOT available

   //Now we set the boolean for the profile and save it
   let didEnter: boolean = true;
   profile = didEnter;
   this.dbProvider.saveProfile(profile);
  //
    let toast = this.toaster.create({
    message: ‘message.’,
    showCloseButton: true,
    duration: 4000,
    cssClass: ‘success’,
    position: ‘bottom’,
    closeButtonText: ‘Close’,
    });
  toast.present();
  }


That is basicly it. You can modify this code also for local storage and such. The main thing what you are doing here is to ask if the boolean exists and if it exists it asks if it is true or false. If its false the message shows and sets the boolean. After that when you enter the page again the message won’t appear since the boolean is set to true.

Hope this helps you :four_leaf_clover::blush:

I am using firebase, yes.

Thank you so much for this, i’ll take a look now but this seems to be exactly what i need

:smiley:

Isn’t it better to use ionViewDidLoad() ?

Code in this function only executes once when the page is loaded and stored in memory. Or maybe it doesn’t fit what you need it for?

https://blog.ionicframework.com/navigating-lifecycle-events/

No it won’t be better, if you load the Page once and don’t pop it, the Page won’t “load” again if you reenter it. But if you use didEnter it will always refresh that section when you enter the Page.

Correct me when I am wrong :blush:

Ahh yes, misread the question. Thought it was just at runtime and not stored in db.