0

My application is using remote notification to remind users of planned actions. User may be informed even after a few months.

Application is updating device token during each time it starts (sends the received token to the server).

But there is a problem. Sometimes device token becomes invalid. (backend-service got error "Invalid token" from APNS). I know that it's normal that device token can change. But there is a case when user set reminder on after a few months and doesn't use app during this time.

How do I update device token when it was expired and when application is turned off?

matulik
  • 41
  • 2
  • My guess would be that the user deleted the app. If you get an invalid token response all you can do is remove it from your database and move on. – Paulw11 Nov 08 '18 at 11:53
  • and who tell you that device token gets updated ? here is a reference that device token is always same for same device. https://stackoverflow.com/questions/6652242/does-the-apns-device-token-ever-change-once-created – Abu Ul Hassan Nov 08 '18 at 11:58
  • [Documentation](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html): APNs can issue a new device token for a variety of reasons: User installs your app on a new device, User restores device from a backup, User reinstalls the operating system, Other system-defined events, – matulik Nov 08 '18 at 12:16

1 Answers1

-1

At every time app running it will call below AppDelegate method, So you need to send the APN token every time app run:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }

        let token = tokenParts.joined()
        print("token :: ", token)
        //TODO:- now you need to send above token to your API that saved this token to user profile


    }
Moayad Al kouz
  • 1,342
  • 1
  • 9
  • 19
  • Yes, I'm doing it, but what when user set reminder on after a few months and doesn't use app during this time? – matulik Nov 08 '18 at 11:51
  • maybe this mean that the user has delete the app from his device, So you should delete the token from your database – Moayad Al kouz Nov 08 '18 at 12:11
  • @matulik if user doesn't use app for few months he will get notification anyway. But if you user restore device and token changed you need save users on server side in other way like email address and token, or facebook id and token – canister_exister Nov 08 '18 at 14:51
  • 1
    this method wont call every time that app runs, only once get called when user allowed push notification alert – Mohammad Reza Koohkan Feb 03 '20 at 08:47