Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity Mobile App - How to send notification when timer ends?

Discussion in 'Getting Started' started by DeXoteric, Nov 4, 2018.

  1. DeXoteric

    DeXoteric

    Joined:
    May 19, 2018
    Posts:
    4
    Hi!

    I need to learn few things for my new project and I don't even know where to start.

    I'm trying to figure out how to keep timer running even when app is closed and when timer is zero I want to send notification.
    For example user taps on icon to start building a mine, it takes xx minutes to finish building, when it's done and app is closed notification is send to user.

    How?

    Is Firebase good for that? Or should I look at something else?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    If you want to do this on mobile, to keep checking time and do some tasks in background, is rather big NO.
    As probably yourself don't want any app running in background, which may drain your battery, same will be for other people.

    Your best option is to validate checks only, when user is opening a game.
     
  3. You can develop background service. AFAIK Unity does not support this, so you will need to create it using native tools.

    But I don't know too much about mobile development since I'm not a mobile developer.
     
  4. DeXoteric

    DeXoteric

    Joined:
    May 19, 2018
    Posts:
    4
    Hmm I think this was simpler than I thought. Any decent plugin capable of scheduling local notification should be good enough for what I'm trying to do.
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    But how would that be expected to work if you have closed your app? "keep timer running even when app is closed"
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    I am not expert in that area, but I think OP referring to some running background tasks.
     
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Exactly. When you close a Unity game, there are no running background tasks, which is my point.
     
  8. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I was going to stay out of this one since it's not something I'm an expert on, but I don't feel like the responses have really pointed the OP in the correct direction yet.

    @DeXoteric you have two options:
    1. Manage all your timers and checks in the game. You'll have to grab timestamps and calculate when timers have expired, but this way does not let you send notifications when the game isn't running.
    2. Create a web service to manage your timers. When a timer gets initiated, make an entry in a database on a web server you operate (or a cloud service like AWS or Firebase or something). That server will be running a scheduled task pretty much constantly to check and see if any timers have expired. If they have, they should reach out to the appropriate push notification service to send the notice to the user's device.
    This is something of a complex process, and it isn't really cheap, either. Which is why you don't tend to see push notifications and timers in smaller indie games.

    So I'll use this post as a soapbox to push my agenda: Why do you want to use this mechanic anyway? Is it because it's found commonly in popular F2P games? Does that make it a good gameplay mechanic, or is it used because it's an effective psychologically exploitative way to encourage people to spend money? Would your game benefit from that manipulative tactic without having the huge install base those games are guaranteed with their massive marketing budgets?

    It's my belief that mimicking what are seen as "successful" tactics from large studios won't work for small fish like us. Not only that, but it lowers the quality of the content we produce, and keeps us stuck in a cycle of uninspired, copycat games.
     
    Bill_Martini likes this.
  9. DeXoteric

    DeXoteric

    Joined:
    May 19, 2018
    Posts:
    4
  10. lagzilajcsi

    lagzilajcsi

    Joined:
    Jan 19, 2020
    Posts:
    9
    Hello, I am trying to do the same thing here.

    Player builds a building, it takes 5 minutes and if Player quits it gets a notification it's ready.
    Could you please share some code or point me in the right direction?

    I am stuck at running time while Player is quit...
     
    Last edited: Feb 23, 2020
  11. Melanzue

    Melanzue

    Joined:
    May 12, 2020
    Posts:
    8
    I'm a little late, but here's how I did it. The timer won't run in the background (that's stupid) but instead you'll subtract time when the game is opened again.
    1. When a player starts building a mine: create a timespan with x time and schedule a notification that fires after an x time.
    2. Write a countdown code that checks if it is 0 or less. Use Update to do it.
    3. When a player closes or puts the game in the background: get DateTime.Now and save it. Use OnApplicationPause to do it.
    4. When a player opens the game again: get DateTime.Now, subtract its ticks from the saved DateTime's ticks and create a new TimeSpan with these ticks. Use OnApplicationResume to do it.
    5. Now the code from line 2 will check if the countdown is more, equal, or less than 0.
    You only have to schedule a notification at the start. It'll work even if the game's closed.