Swift iOS 13, Not getting APNS Device token while using Mobile Network (4g/3g)
Asked Answered
K

2

6

I was trying to get APNS push token.

func configPushNotifications(_ application: UIApplication) {
    application.registerForRemoteNotifications()
}

But I didn't received any Token from AppDelegate if I am using My Phone Sim Internet (4g/3g).

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

But if i use Wifi, it's working fine. I checked iOS 13.1.2 and 13.1.3. Both have the same problem. But lower versions like iOS 12 or 11 working fine. Is it apple bug? or I have to request token with different config for Mobile network?

Karalynn answered 17/10, 2019 at 6:15 Comment(6)
Terms out, may be Apple doesn't received APNS push token request. May be it blocked by some Network operator. I changed my current Network operator and its start working again. And app successfully received APNS Push token. If anyone knows any solution for this problem? How to make sure Apple received APNS token request without any blocking?Karalynn
Faced same issue. Can u guide me, how to solve this issue ??Thorne
Having the same issue, iOS 13.4.1, cannot get device token, no error, how to get device token?Staggers
Could you try with release ipa? It's interesting for me, i didn't get push token in dev build, but while using release provisioning profile and release ipa, I did receive push token without issue.Karalynn
I'm having the same issue right now. Got it someone Working with developer profile?Hurwit
14.4 the same issue, failed to register. And the same build is fine on iOS 12. I spent few days before tried on iOS 12. No solution yet ((Macias
W
3

Please verify code as looks like this

first import local notification

import UserNotifications

then create a method

func settingPushNotification() {
    
    let app = UIApplication.shared
    
    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        app.registerUserNotificationSettings(settings)
    }
    
    app.registerForRemoteNotifications()
}

you can call this method either in appdelegate or in viewcontroller this way.

self.settingPushNotification()

you need to add delegate methods

func application(
    _ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
    ) {
    let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
    let token = tokenParts.joined()
    
    if !token.isEmpty {
        
        let userDefaults = UserDefaults.standard
        userDefaults.set(token, forKey: Strings.DeviceToken.rawValue)

    }
    

    print("Device Token: \(token)")
}

func application(
    _ application: UIApplication,
    didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Failed to register: \(error)")
}

Make sure you added push notification in signing and capabilities.

enter image description here

This way you can get APNS Device token.

Wool answered 17/10, 2019 at 8:37 Comment(6)
UserNotifications request was done as it is. As I said, it's work's normally for older iOS version. Only issue in Updated iOS 13.1.2 & 13.1.3 only if I connected to Mobile Internet (4g/3g). And If i was not add Remote Notification in Signing & Capabilities, then older iOS also should not work properly.Karalynn
Faced same issue. Can u guide me, how to solve this issue ?Thorne
@McDonal - Using dev build, i didn't get push token for few network operator. But when i checked with release build, push token did received from APNS without any issue. Could you please check? Or for dev build testing you can try with wi-fi network.Karalynn
I understood ur comments . But my situation is I am having only one testing device iOS 13.1.3 . Now, I couldn’t develop push notification with that device. I have tried with network operator and wifi too. No use . Is there anyway to solve this ?Thorne
@Thorne Did you find any solution? I have been unable to identify reason that why push notifications randomly stop working on ios 13 or below devices :/Gyron
No, I couldnt find yet.Thorne
H
0

If you are using Firebase, try adding the following to your Info.plist:

<key>FirebaseAppDelegateProxyEnabled</key>
<false />
Hashim answered 27/2 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.