Ios App&x27;s Firebase推送通知在iPhone上成功,但在iPad上失败
我正在将Firebase推送通知用于我的应用程序的推送通知。对于使用iphone的用户来说,一切正常,但对于使用ipad的用户来说,一切都失败了。下面是我如何实现它的。是否有一些错误或需要补充的内容?多谢各位Ios App&x27;s Firebase推送通知在iPhone上成功,但在iPad上失败,ios,iphone,ipad,push-notification,swiftui,Ios,Iphone,Ipad,Push Notification,Swiftui,我正在将Firebase推送通知用于我的应用程序的推送通知。对于使用iphone的用户来说,一切正常,但对于使用ipad的用户来说,一切都失败了。下面是我如何实现它的。是否有一些错误或需要补充的内容?多谢各位 class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { let db = SQLiteDB.shared func userNotificationCenter(_
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
let db = SQLiteDB.shared
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
_ = db.open()
FirebaseApp.configure()
UNUserNotificationCenter.current().delegate = self
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization( options: authOptions,completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
print("Messaging.messaging().fcmToken= ",Messaging.messaging().fcmToken)
return true
}
........
}
扩展AppDelegate:MessagingDelegate{
func消息传递(u消息传递:消息传递,didReceiveRegistrationToken fcmToken:String){
let dataDict:[String:String]=[“token”:fcmToken]
NotificationCenter.default.post(名称:Notification.name(“FCMToken”),对象:nil,用户信息:dataDict)
}
func消息传递(uMessaging:messaging,didReceive remoteMessage:MessagingRemoteMessage){
}
func应用程序(application:UIApplication,DidRegisterForRemotionTificationswithDeviceToken deviceToken:Data){
Messaging.Messaging().apnsToken=deviceToken
}
func userNotificationCenter(center:UNUserNotificationCenter,didReceive响应:UNNotificationResponse,withCompletionHandler completionHandler:@escaping()->Void){
让application=UIApplication.shared
如果application.applicationState==.active{
打印(“用户点击前台的通知”)
}
如果application.applicationState==.inactive{
打印(“用户在后台点击通知”)
}
让userInfo=response.notification.request.content.userInfo
guard let aps=userInfo[“aps”]as?Dictionary else{return}
completionHandler()
}
}
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let application = UIApplication.shared
if application.applicationState == .active {
print("user tapped the notification in foreground")
}
if application.applicationState == .inactive {
print("user tapped the notification in background")
}
let userInfo = response.notification.request.content.userInfo
guard let aps = userInfo["aps"] as? Dictionary<String, Any> else { return }
completionHandler()
}