I had a question, here there are applications that send notifications like "Вы не заходили в приложение уже 2 дня " and the like. Do these applications also send notifications through their own servers?

There is no possibility to create local notifications with a timer, what would be allowed an hour after the application was closed, a notification was shown?

If not, do you get monitoring of user inactivity is also determined by the last connection to the server?

    3 answers 3

    You can send pushes by the last connection, or you can run local notifications. For example, if the user has turned off the application, run the notification,

     func applicationDidEnterBackground(_ application: UIApplication) { let content = UNMutableNotificationContent() content.title = "Notification" content.body = "Sample" let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false) //покажет через 5 секунд let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request){(error) in if (error != nil){ print(error?.localizedDescription ?? "") } } } 

    if the user has opened the application, we kill it.

     func applicationDidBecomeActive(_ application: UIApplication) { UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [requestIdentifier]) } 

    If you did not run a specific time, it is shown.

    GitHub Example

    • And if the interval is not 5 seconds. put a 5 days, and in the course of these 5 days to restart the phone. will this code notify? - Murad
    • 2
      @Murad interesting script. I checked it for 5 minutes, after the reboot I received a notification. - VAndrJ

    Remote push notifications can be sent from their own servers at any time. There are two options

    1. Directly send a message to APNS at gateway.sandbox.push.apple.com:2195
    2. Use a third-party service, for example http://firebase.google.com You can send directly via the Firebase console (Notifications-> New message), or POST request to their server.

    Your task can be implemented in both ways.

    Through remote notifications - in the application on the backend you constantly save the last time activity

    Through local - with the necessary interval, you re-create the only notification that will work after the period you need.

    Both notifications will work if the application is closed.

      This is usually done using UILocalNotification. Each time you enter the application, erase the notification and set a new one for 2 days.

       - (void)applicationDidBecomeActive:(UIApplication *)application { [[UIApplication sharedApplication] cancelAllLocalNotifications]; application.applicationIconBadgeNumber = 0; UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2*24*60*60]; localNotification.alertBody = @"Вы не заходили в приложение уже 2 дня"; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.applicationIconBadgeNumber = 1; localNotification.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];}