[SWIFT] Push notification permission pop-up does not appear on iOS14

Error details

It seems to be a problem peculiar to iOS14, but if the ProductName of the app contains a specific character (probably other than the alphabet), it will be used when implementing Push notification. ʻUNUserNotificationCenter.current (). requestAuthorization (options :, completionHandler :) `The method is flipped with an error and the pop-up asking the user for permission to notify disappears.

In my case, I put hiragana in ProductName (why I put it in ...), and I was addicted to it because the pop-up did not appear, so I will describe the details of the error and the solution.

You can find all the details of this issue in the Apple Developer Forums. If you are fluent in English, I think it is faster to read the link. https://developer.apple.com/forums/thread/660715

Code and error details

AppDelegate


import UIKit
import UserNotifications

    ***

class AppDelegate: UIResponder, UIApplicationDelegate {

    ***

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 
        
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { granted, error in
            print(error)
            guard error == nil else { return }
            if granted {
                DispatchQueue.main.async {
                    application.registerForRemoteNotifications()
                }
            }
        })

        return true
    }
    
}

When sending push notifications to users, use the requestAuthrization () method of ʻUNUserNotificationCenter to ask for permission, but if there is a problem with ProductName, it will be played with an error in completionHandlerandregisterForRemoteNotifications ()` will be executed. The pop-up display is not executed.

If you check the contents with print (error), it will be displayed on the console as follows.

Error Domain=UNErrorDomain Code=1 "Notifications are not allowed for this application"

Solutions

  1. Change the ProductName in the TARGETS-> BuildSettings-> Packaging item to something that works (officially says simple ASCII characters). The Product Name is invisible to the user, so you don't have to think too much about it.

  2. If you want to use hiragana or kanji in a place that the user can see (such as the app name under the app icon), add an item called Bundle Display Name to Info.plist and enter the app name you want to display there. To do.

  3. The pop-up was displayed without any problem. 3E638F8B-D27B-4E31-9FFF-522DB5D96F0F_1_201_a.jpeg

that's all.

Recommended Posts

Push notification permission pop-up does not appear on iOS14
Receive parameterized PUSH notifications on iOS
UriComponentsBuilder does not encode "+" depending on spring version
[React + TypeScript] Property'options' does not exist on type'HTMLElement'.