为什么我不能从Swift的Firebase推送通知? [英] Why I do not get push notifications from Firebase in Swift?
问题描述
pre $ class $ AppDelegate:
func application(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool {
if #available(iOS 10.0,*){
let authOptions:UNAuthorizationOptions = [.Alert,.Badge,.Sound]
UNUserNotificationCenter.currentNotificationCenter()。requestAuthorizationWithOptions(authOptions,completionHandler :在
中{{granted:Bool,error:NSError?}对于iOS 10显示通知(通过APNS发送)
UNUserNotificationCenter.currentNotificationCenter()。delegate = self
//对于iOS 10数据信息(通过FCM发送)
FIRMessaging.messaging()。remoteMessageDelegate = self
})
} else {
设置:UIUserNotificationSettings =
UIUserNotificationSettings(forTypes :[.Alert,.Badge,.Sound],类别:无)
application.registerUserNotificationSettings(设置)
}
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert,.Sound,.Badge],类别:无))
}
另外这部分内容:
pre $内部func应用程序(application:UIApplication,didReceiveRemoteNotification userInfo:[NSObject:AnyObject]){
print(userInfo)
//如果在应用程序处于后台时收到通知消息,
//只有当用户点击启动应用程序的通知时,才会触发此回调。
// TODO:处理通知数据
//打印消息ID。
print(Message ID:\(userInfo [gcm.message_id]!))
//打印完整的信息。
print(userInfo)
func application(application:UIApplication,didReceiveRemoteNotification userInfo:[NSObject:AnyObject],
fetchCompletionHandler completionHandler:(UIBackgroundFetchResult) - >无效){
//如果您的应用程序在后台收到通知消息,
//只有当用户点击启动应用程序的通知时,才会触发此回调。
// TODO:处理通知数据
//打印消息ID。
print(Message ID:\(userInfo [gcm.message_id]!))
//打印完整的信息。 (中心:UNUserNotificationCenter,willPresentNotification通知:UNNotification,withCompletionHandler completionHandler:(UNNotificationPresentationOptions());
print(userInfo)
}
可用(iOS 10.0,*)
func userNotificationCenter ) - > Void){
//处理通知
}
@available(iOS 10.0,*)
func userNotificationCenter(中心:UNUserNotificationCenter,didReceiveNotificationResponse响应:UNNotificationResponse,withCompletionHandler completionHandler:() - > Void){
//处理通知
}
我也将开发APN上传到Apple开发人员。在Firebase控制台中,它写道通知已成功发送,但我没有收到任何推送通知。任何人都可以请帮我检测我的错误是哪里?
我已经工作了3天。搜索了很多,但没有。
对于任何仍在寻找的人,请查看这里的最后一个答案:ios10,Swift 3和Firebase推送通知(FCM)
- 检查您的推送通知权利并将其打开。目标>功能>推送通知。
I set this code in my AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.Alert, .Badge, .Sound]
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(authOptions, completionHandler: { (granted: Bool, error: NSError?) in
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.currentNotificationCenter().delegate = self
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
})
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil))
}
Also this parts:
internal func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
// 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
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print(userInfo)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (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
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print(userInfo)
}
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
//Handle the notification
}
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
//Handle the notification
}
I also uploaded development APNs into the Apple Developers. In the Firebase console it writes that notification was successfully sent, but I do not get any push notification. Can anyone please help me to detect where my mistake is?
I have worked on it for 3 days. Searched a lot, but nothing.
For anyone still looking, check the last answer here: ios10, Swift 3 and Firebase Push Notifications (FCM)
- Check your push notification entitlement and switch it on. Target > Capabilities > Push notifications.
这篇关于为什么我不能从Swift的Firebase推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!