-3

If i haven't allow notification when app asked for it. Then I went to setting and manually allow the notification.

So the question is how can i get notification token without kill and restart the app. Is there a way to get token without restart the app.

I have register for the notification in

  • Application didfinish with option method
  • So Got the popUp to get notification allow or Not. At that time I have deny it.
  • Now I went to setting of app and enable that notification for the app. But didnot restart the app.
  • In that case is there a way to get the notification token. (Though the app is not relaunching when i change the notification state manually in iOS 13)
  • 1
    You don't need permission to obtain the remote notification token; you can just request it. You do need permission to display alerts; If you send a push notification and you haven't got that permission then the alert won't be shown. – Paulw11 Jun 20 '19 at 12:24
  • @Paulw11 Thanks for the answer. But I need to know how can i retrive device token when you allow notification from setting and then come back to application. Is there any way to retrive the token without relaunch the app. Please let me know as I got stuck in this for 4 hours. – Jasmine Chaniara Jun 20 '19 at 12:34
  • 1
    This might help https://stackoverflow.com/a/10191220/285190 – Flexicoder Jun 20 '19 at 12:36
  • 1
    Why do you care? Just get the token and register it with your server. The user can change whether notifications are actually shown at any time; your server always sends the notification; if the user has disabled notifications then the push is delivered to your app silently. – Paulw11 Jun 20 '19 at 12:41
  • @Paulw11@Flexicoder Thanks for your time. Got the point – Jasmine Chaniara Jun 20 '19 at 13:05
  • @Paulw11 Actually my requirement is to send notification token when user manually enable the notification from setting and will not relaunch the app. As if app relaunch did register for notification will be called and I'll get notification. But it all when app relaunch. what if app is just in background and enter into foreground again. it will not called my did register for notification method. Got my point ? – Jasmine Chaniara Jun 20 '19 at 13:13

2 Answers2

1

When user comes back to the app (e.g. in function applicationWillEnterForeground())
call this: UIApplication.shared.registerForRemoteNotifications().

If user has turned on Push Notification in Settings,

func application(_ application: UIApplication, 
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

will be called and you can get the device token there.

myeongkil kim
  • 2,465
  • 4
  • 16
  • 22
Luke
  • 11
  • 1
-1

You should not worry about APNs token in your case.
Apple send token independently of approve notifications or not. https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/HandlingRemoteNotifications.html#//apple_ref/doc/uid/TP40008194-CH6-SW1

In iOS and tvOS, you initiate APNs registration for your app by calling the registerForRemoteNotifications method of the UIApplication object. Call this method at launch time as part of your normal startup sequence. The first time your app calls this method, the app object contacts APNs and requests the app-specific device token on your
behalf.

And if token changed, you don't need relaunch app.

If the device token changes while your app is running, the app object calls the application:didRegisterForRemoteNotificationsWithDeviceToken: delegate method again to notify you of the change.

Update
Maybe i don't understand your question.
But step by step:

  1. Application did register for notification
  2. User see system alert about Notification
  3. User deny Notifications
  4. Application anyway get device token for APNs
  5. Application send token to any service (Your server api, Firebase, OneSignal e.t.c.)
  6. Your server anyway send Notifications to APNs (but user not see this Notifications)
  7. User open Application Setting and tern on Notification
  8. You didn't do anything
  9. Your server anyway send Notifications to APNs
  10. Notification just showing for users and arrived in Application

Update

This answer is deprecated, sorry for your time

Eysner
  • 584
  • 4
  • 15
  • Hello @Aisner , I want to knew that when we are taking permission from user of remote notification and user deny it first. Then from setting it on notification. Then how can we get notification. – Jasmine Chaniara Jun 24 '19 at 08:37
  • Hi @JasmineChaniara, i update answer. You have question with step by step part? – Eysner Jun 24 '19 at 08:58
  • Wrong answer, Point 4 is totally false – Mehul Feb 14 '23 at 13:17
  • About point 4 - i think it behavior did change, you can check it with answers about old version iOS. If you know, how to need update answer, just let me know how. Anyway thanks for info. @Mehul – Eysner Feb 14 '23 at 17:11
  • No worry. Thanks for note "Update", It helps others. @Eysner – Mehul Feb 15 '23 at 10:41