带Firebase通知的iOS,全部不工作
无论我做什么,都没有收到通知。 发送到Android很正常,发送到iOS是失败的 我从Firebase控制台发送 我没有收到前台或后台通知 这是AppDelegate.swift带Firebase通知的iOS,全部不工作,ios,swift,firebase,firebase-cloud-messaging,Ios,Swift,Firebase,Firebase Cloud Messaging,无论我做什么,都没有收到通知。 发送到Android很正常,发送到iOS是失败的 我从Firebase控制台发送 我没有收到前台或后台通知 这是AppDelegate.swift import UIKit import Firebase import FirebaseMessaging import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UNUser
import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if #available(iOS 10.0, *) {
UIView.appearance().semanticContentAttribute = .forceLeftToRight
}
FirebaseApp.configure()
// [START set_messaging_delegate]
Messaging.messaging().delegate = self as? MessagingDelegate
// [END set_messaging_delegate]
//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 })
// For iOS 10 data message (sent via FCM)
//FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var token = ""
for i in 0..<deviceToken.count {
token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
}
print("Registration succeeded! Token: ", token)
let topicName = "/topics/ChaclateOnMobile"
Messaging.messaging().subscribe(toTopic: topicName)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Registration failed! \(error)")
}
// Firebase notification received
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
// custom code to handle push while app is in the foreground
print("Handle push from foreground\(notification.request.content.userInfo)")
let dict = notification.request.content.userInfo["aps"] as! NSDictionary
let d : [String : Any] = dict["alert"] as! [String : Any]
let body : String = d["body"] as! String
let title : String = d["title"] as! String
print("Title:\(title) + body:\(body)")
self.showAlertAppDelegate(title: title,message:body,buttonTitle:"ok",window:self.window!)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
print("Handle push from background or closed\(response.notification.request.content.userInfo)")
}
func showAlertAppDelegate(title: String,message : String,buttonTitle: String,window: UIWindow){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.default, handler: nil))
window.rootViewController?.present(alert, animated: false, completion: nil)
}
// Firebase ended here
导入UIKit
进口火基
导入FirebaseMessaging
导入用户通知
@UIApplicationMain
类AppDelegate:UIResponder、UIApplicationLegate、UNUserNotificationCenterDelegate{
变量窗口:UIWindow?
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptions:[UIApplicationLaunchOptions:任何]?)->Bool{
//应用程序启动后自定义的覆盖点。
如果可用(iOS 10.0,*){
UIView.appearance().semanticContentAttribute=.forceLeftToRight
}
FirebaseApp.configure()
//[启动设置\u消息\u委托]
Messaging.Messaging().delegate=self-as?MessagingDelegate
//[结束集\u消息\u委托]
//创建通知中心
如果可用(iOS 10.0,*){
//对于iOS 10显示通知(通过APNS发送)
UnuseNotificationCenter.current().delegate=self
let authOptions:UNAuthorizationOptions=[.alert、.badge、.sound]
UnuseNotificationCenter.current().requestAuthorization(
选项:authOptions,
completionHandler:{{{,{in})
//对于iOS 10数据消息(通过FCM发送)
//FIRMessaging.messaging().remoteMessageDelegate=self
}否则{
let设置:UIUserNotificationSettings=
UIUserNotificationSettings(类型:[.alert、.badge、.sound],类别:nil)
application.registerUserNotificationSettings(设置)
}
应用程序.注册表项更改()
返回真值
}
func应用程序(application:UIApplication,DidRegisterForRemotionTificationswithDeviceToken deviceToken:Data){
var token=“”
对于0中的i..Void){
//在应用程序处于前台时处理推送的自定义代码
打印(“从前台处理推送\(notification.request.content.userInfo)”)
让dict=notification.request.content.userInfo[“aps”]as!NSDictionary
设d:[String:Any]=dict[“alert”]as![String:Any]
让body:String=d[“body”]as!String
将title:String=d[“title”]设为!String
打印(“标题:\(标题)+正文:\(正文)”)
self.showAlertAppDelegate(标题:标题,消息:正文,按钮:“确定”,窗口:self.window!)
}
@可用(iOS 10.0,*)
func userNotificationCenter(center:UNUserNotificationCenter,didReceive响应:UNNotificationResponse,withCompletionHandler completionHandler:@escaping()->Void){
//如果您在DidReceiveMemoteNotification中设置了一个成员变量,您将知道这是来自closed还是background
打印(“处理后台推送或关闭\(响应.通知.请求.内容.用户信息)”)
}
func showAlertAppDelegate(标题:字符串、消息:字符串、按钮:字符串、窗口:UIWindow){
let alert=UIAlertController(标题:标题,消息:消息,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:Buttontile,样式:UIAlertActionStyle.default,处理程序:nil))
window.rootViewController?.present(警报、动画:false、完成:nil)
}
//火力基地就在这里结束了
}
令牌
发送通知AppDelegate中实现了正确的方法。
未运行时
,会在didfishlaunchingwithoptions
中收到通知
将此项添加到代码中的didfishlaunchingwithoptions
:
if let notification = launchOptions?[.remoteNotification] as? [String: AnyObject]
{
//Your code
}
当您的应用程序位于后台/前台
时,会在didReceiveMemoteNotification
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
//Your code
}
检查您是否使用正确的令牌发送通知。1.是否启用了来自Xcode功能的推送通知?2.是否启用了来自后台模式的远程通知(也在Xcode功能中)?有关需要执行的操作的完整列表,请参阅