我正在为工作中的应用程序设置iOS 12推送通知。它们在前两天工作得很好,然后突然完全停止了。我无法访问firebase服务器。我通过post发送api密钥和设备id,web应用程序在服务器上注册该设备。当我第一次设置它时,它对我来说工作得很好,然后突然它就完全停止工作了。由于某些原因,它在Android上运行得很好。
我尝试了很多解决方案,除了没有设置apns令牌,当我没有设置apns令牌时,firebase didrecievemessage会被调用,而不是苹果默认的那个。如果我设置了APNS标记,则不会调用didReceive remoteMessage!我没有得到任何错误。下面的代码是在多次之后,甚至检查出以前工作的版本也没有做任何事情。appcoord是设置我的视图控制器的类。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
appCoord.start()
UNUserNotificationCenter.current().delegate = self
application.registerForRemoteNotifications()
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {result,error in print(result,error) })
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
InstanceID.instanceID().deleteID{ error in
Messaging.messaging().setAPNSToken(deviceToken, type: .prod)
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true
}
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
KeychainProvider.setToken(token: fcmToken)
print(KeychainProvider.loadToken())
appCoord.updateToken()
Messaging.messaging().subscribe(toTopic: "news")
print("gottoken")
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
appCoord.updateInbox()
print("msg",remoteMessage)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("dobio je notifikaciju")
appCoord.updateInbox()
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("fjyfjdfj--------------------------------------")
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let notificationType = UIApplication.shared.currentUserNotificationSettings?.types
print("push enabled")
// 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]
var body : String = d["body"] as! String
let title : String = d["title"] as! String
print("Title:\(title) + body:\(body)")
completionHandler([.alert,.badge,.sound])
}
发布于 2019-09-09 13:09:20
如果它以前工作,但突然停止,一个特定的情况可能是如果您在开发和生产期间在同一个iOS设备上测试它。如果收到实际的生产通知,它可能会停止在开发中工作。如果是这样,那就在还没有用过这款应用的iOS设备上试试吧。
https://stackoverflow.com/questions/57854490
复制相似问题