我目前正在尝试获取推送消息。但是,您不会收到推送消息。我想念什么?
AppDelegate
import UIKit
import UserNotifications
import Firebase
import FirebaseMessaging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
// Override point for customization after application launch.
//create the notificationCenter
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)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
Messaging.messaging().delegate = self
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map{ String(format: "%02x", $0) }.joined()
Log.Info("Registration succeeded!")
Log.Info("Token: \(token)")
LocalStorage.set(token, "dacDeviceToken")
Messaging.messaging().apnsToken = deviceToken
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
Log.Error("Error fetching remote instance ID: \(error)")
} else if let result = result {
Log.Info("Remote instance ID token: \(result.token)")
}
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Log.Warning("Registration failed!")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo["gcmMessageIDKey"] {
Log.Info("Message ID: \(messageID)")
}
// Print full message.
Log.Info(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo["gcmMessageIDKey"] {
Log.Info("Message ID: \(messageID)")
}
// Print full message.
Log.Info(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print message ID.
Log.Info("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print("%@", userInfo)
Log.Info(userInfo)
}
}
extension AppDelegate : MessagingDelegate {
// Receive data message on iOS 10 devices.
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage) {
print("%@", remoteMessage.appData)
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
Log.Info("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
}
我的日志
2019-09-24 19:31:46.519806 + 0900测试[586:74065]-
[I-ACS036002]分析屏幕报告已启用。
调用+ [FIRAnalytics setScreenName:setScreenClass:]设置屏幕
名称或覆盖默认屏幕类名称。禁用屏幕
报告,将标志FirebaseScreenReportingEnabled设置为NO(布尔值)
在Info.plist 2019-09-24 19:31:46.756433 + 0900测试[586:74071]
6.9.0-[Firebase / Messaging] [I-FCM001000] FIRMessaging远程通知代理已启用,将使远程通知接收者陷入困境
处理程序。如果您希望手动集成Firebase Messaging,
将“ FirebaseAppDelegateProxyEnabled”添加到您的Info.plist中,并进行设置
至否。请按照以下说明进行操作:
以确保正确集成。 2019-09-24 19:31:46.759687 + 0900
test [586:74071] 6.9.0-[Firebase / Analytics] [I-ACS023007] Analytics(分析) v.60102000开始于2019-09-24 19:31:46.760699 + 0900测试[586:74071] 6.9.0-[Firebase / Analytics] [I-ACS023008]要启用调试日志记录,请设置以下应用程序参数:-FIRAnalyticsDebugEnabled INFO:2019-09-24 10:31:46 +0000- AppDelegate.swift消息传递(_:didReceiveRegistrationToken :) [Line:196]
Firebase注册令牌: dZ4US-5dJqk:APA91bF0-**************** 信息:2019-09-24 10:31:46 +0000-AppDelegate.swift 应用程序(_:didRegisterForRemoteNotificationsWithDeviceToken :) [Line:82]注册成功!信息:2019-09-24 10:31:46 +0000- AppDelegate.swift
application(:didRegisterForRemoteNotificationsWithDeviceToken :) [Line:83]令牌: 213eba827 **********************************信息: 2019-09-24 10:31:46 +0000-AppDelegate.swift 应用程序(:didRegisterForRemoteNotificationsWithDeviceToken :) [Line:90]远程实例ID令牌: dZ4US-5dJqk:APA91bF0-77 ***********
2019-09-24 19:31:46.921546 + 0900测试[586:74071] [MC]系统组 systemgroup.com.apple.configurationprofiles路径的容器是 /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2019-09-24 19:31:46.923537 + 0900测试[586:74071] [MC]阅读 公开的有效用户设置。
我什么都没有。无论是在前台还是在后台,我的应用程序都不会收到推送消息。
请帮我很多忙。
您在图中添加的令牌是设备令牌值。令牌值在日志中可见。
我看到了答案并遵循了答案,但是它没有用。
答案 0 :(得分:1)
您可以在项目中放入 GoogleService-Info.plist 文件吗?
在我的代码中尝试此方法100%工作
import Firebase
import FirebaseCore
import FirebaseMessaging
import UserNotifications
import UserNotificationsUI
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate , MessagingDelegate{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Messaging.messaging().delegate = self
FirebaseApp.configure()
//Register App For Push Notification
self.registerAppForPushNotificaition()
application.registerForRemoteNotifications()
return true
}
func registerAppForPushNotificaition(){
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
let inviteCategory = UNNotificationCategory(identifier: "Notification", actions: [], intentIdentifiers: [], options: UNNotificationCategoryOptions.customDismissAction)
let categories = NSSet(objects: inviteCategory)
center.delegate = self
center.setNotificationCategories(categories as! Set<UNNotificationCategory>)
center.requestAuthorization(options: [.sound, .badge, .alert], completionHandler: { (granted, error) in
if !(error != nil){
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
})
} else {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types:[.sound , .alert , .badge] , categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler(.alert)
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)
Messaging.messaging().apnsToken = deviceToken
let token = InstanceID.instanceID().token()
if token != nil {
fcmID = token!
}
}
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
// print(deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NSLog("Failed to get Access Token: \(error)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(userInfo) {
completionHandler(UIBackgroundFetchResult.noData)
return
}
completionHandler(UIBackgroundFetchResult.newData)
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("fcmToken \(fcmToken)")
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("remort \(remoteMessage.appData)")
}
答案 1 :(得分:1)
我看到了答案并遵循了答案,但是它没有用。但是,当您在Fire Base上添加新项目,再次添加iOS应用并尝试后,它成功了。我不知道怎么了。我的代码有问题吗?我想,但是当我在问题中使用代码时,它也起作用了。我只是为解决方案创建并添加了一个新项目。