eli*_*lia 8 apple-push-notifications firebase swift firebase-cloud-messaging
我目前正在处理通知,但我没有成功从 Firebase FCM 收到任何通知。
podfile 配置是:
target 'Project' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for Project
pod 'Firebase/Core'
pod 'Firebase/Messaging'
end
Run Code Online (Sandbox Code Playgroud)
我已经激活Push Notifications并Remote Notifications从Background fetches并且已经阅读了stackoverflow当前类似的另一个主题,在这里
我想和你分享我的 AppDelegate 代码,但首先我不得不说谷歌的推送通知文档似乎有点混乱,因为这里有很多覆盖的方法,每个教程都有不同的方式来完成接收通知。
我已经进口了这些
import Firebase
import FirebaseInstanceID
import UserNotifications
import FirebaseMessaging
Run Code Online (Sandbox Code Playgroud)
然后是代表团
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{
...
}
Run Code Online (Sandbox Code Playgroud)
然后这里是 willFinishLaunchWithOptions 方法
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
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 })
// For iOS 10 data message (sent via FCM
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
Run Code Online (Sandbox Code Playgroud)
这是Messaging委托功能。
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("MEssage -> \(remoteMessage.appData)")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
Run Code Online (Sandbox Code Playgroud)
该函数在 Firebase 文档中用于设置 apns 令牌。
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}
Run Code Online (Sandbox Code Playgroud)
我已经从 FCM' 发送了数百个通知,它在服务器端成功发送,但是当我记录收到的消息时,收入数据没有任何内容。
如果你问我为什么在willFinish函数中实现配置,那么文档中有这样的注释。
For devices running iOS 10 and above, you must assign the
UNUserNotificationCenter's delegate property and FIRMessaging's
delegate property. For example, in an iOS app, assign it in the
applicationWillFinishLaunchingWithOptions: or
applicationDidFinishLaunchingWithOptions: method of the app delegate.
Run Code Online (Sandbox Code Playgroud)
我很感激你的任何帮助,因为现在很难看出它有什么问题。
Yog*_*del 12
请检查您是否在您的项目功能中激活了推送通知
创建开发 APNs 证书并将其添加到 Firebase 控制台项目设置
在您的应用程序委托中
import FirebaseMessaging
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,
UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//REMOTE NOTIFICATION
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()
Messaging.messaging().delegate = self
let token = Messaging.messaging().fcmToken
print("FCM token: \(token ?? "")")
//Added Code to display notification when app is in Foreground
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
} else {
// Fallback on earlier versions
}
return true
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken as Data
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
// Print full message.
print(userInfo)
}
// This method will be called when app received push notifications in foreground
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{ completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge])
}
// MARK:- Messaging Delegates
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
print("Error fetching remote instange ID: \(error)")
} else if let result = result {
print("Remote instance ID token: \(result.token)")
}
}
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("received remote notification")
}
}
Run Code Online (Sandbox Code Playgroud)小智 6
我正在使用 FirebaseMessaging v4.1.4 并启用了方法调配
application.registerForRemoteNotifications() --> 首先
Messaging.messaging().delegate = self --> Second
您必须在请求远程通知后设置消息代理。(我注意到你在请求许可之前调用了它)
就我而言,因为我在 iOS 13 的 didRegisterForRemoteNotificationsWithDeviceToken 方法中没有收到 APNS 令牌之前调用了它,而生成了 firebase 令牌。
也许是因为没有生成 APNS 并且 firebase 没有与 APNS 令牌映射,您可能没有收到任何通知。
| 归档时间: |
|
| 查看次数: |
9218 次 |
| 最近记录: |