🛑

iOS14でPush通知許可のポップアップが出なくなる問題

2022/02/06に公開

※Qiitaから移行した記事です(投稿日2020年11月02日)

エラーの詳細

iOS14特有の問題らしいのですが、アプリのProductNameに特定の文字(おそらくアルファベット以外)が含まれていると、Push通知を実装する時に使う
UNUserNotificationCenter.current().requestAuthorization(options:, completionHandler:)メソッドがエラーで弾かれて、ユーザーに通知の許可を求めるポップアップが出現しなくなります。

自分の場合はProductNameに平仮名を入れており(なぜ入れたのか...)、ポップアップが表示されずハマってしまったのでエラーの詳細と解決方法について記しておきます。

AppleDeveloperのForumsに今回のissueの詳細は全て載っています。英語が大丈夫な方はリンクの方を読んでいただいた方が早いかと思います。
https://developer.apple.com/forums/thread/660715

コードとエラー内容

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
    }
    
}

ユーザーにプッシュ通知を送る時はUNUserNotificationCenterrequestAuthrization()メソッドを使い許可を求めますが、ProductNameに問題があるとcompletionHandlerにてエラーで弾かれ、registerForRemoteNotifications()が実行されず、ポップアップ表示が実行されません。

print(error)で内容を確認してみると以下のようにコンソールに表示されます。

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

解決方法

  1. TARGETS -> BuildSettings -> Packaging項目内にあるProductNameを、問題ないもの(公式にはsimple ASCII charactersとあります)に変更する。
    ProductNameはユーザーの目に触れないものなのであまり深く考えなくても大丈夫です。

  2. ユーザーの目に触れる場所(Appアイコンの下のアプリ名など)に平仮名や漢字を使いたい場合は、Info.plistにBundle Display Nameという項目を追加し、そこに表示させたいアプリ名を入力する。

  3. 問題なくポップアップが表示されました。

GitHubで編集を提案

Discussion