2016-12-15 21 views
8

Tôi biết bạn đã vấp phải câu hỏi này trước đây; nhiều lần trong thực tế. Nhưng tôi đã theo dõi mọi đề xuất được cung cấp cho tôi here, here và thậm chí here.FCM + Swift 3 - Thông báo không xuất hiện

(Được viết bằng Swift 3, chạy trên Xcode 8.1)

Có, thông báo PUSH của tôi về khả năng được bật. Và ngay cả Bối cảnh của tôi khả năng từ xa chế độ là trên - Tôi đã thử chuyển đổi qua lại các FirebaseAppDelegateProxy On và Off, kiểm tra giấy chứng nhận (tại sao, vâng, nó được trỏ đến App bó đúng), di chuyển

application.registerForRemoteNotifications() 

khóc, tiêu thụ một lượng đường phong phú, cầu nguyện với Thiên Chúa và sau đó những gì bao giờ các vị thần liên quan khác tôi có thể nghĩ đến, và vẫn vô ích.

Nó chỉ có thể là một nhu cầu la hét cho một bộ thứ hai của mắt, nhưng bất kỳ ý tưởng?

import UIKit 
import UserNotifications 

import Firebase 
import FirebaseMessaging 


@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    let gcmMessageIDKey = "gcm.message_id" 


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     FIRApp.configure() 
     // Override point for customization after application launch. 

     // Override point for customization after application launch. 
     // [START register_for_notifications] 
     if #available(iOS 10.0, *) { 
      let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound] 
      UNUserNotificationCenter.current().requestAuthorization(
       options: authOptions, 
       completionHandler: {_,_ in }) 

      // For iOS 10 display notification (sent via APNS) 
      UNUserNotificationCenter.current().delegate = self 
      // 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() 

     // Add observer for InstanceID token refresh callback. 
     NotificationCenter.default.addObserver(self, 
                 selector: #selector(self.tokenRefreshNotification), 
                 name: NSNotification.Name.firInstanceIDTokenRefresh, 
                 object: nil) 

     return true 
    } 

    func tokenRefreshNotification(_ notification: Notification) { 
     if let refreshedToken = FIRInstanceID.instanceID().token() { 
      print("InstanceID token: \(refreshedToken)") 
     } 

     // Connect to FCM since connection may have failed when attempted before having a token. 
     connectToFcm() 
    } 

    // [START connect_to_fcm] 
    func connectToFcm() { 
     FIRMessaging.messaging().connect { (error) in 
      if error != nil { 
       print("Unable to connect with FCM. \(error)") 
      } else { 
       print("Connected to FCM.") 
      } 
     } 
    } 
    // [END connect_to_fcm] 

    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 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 

     // Print message ID. 
     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     // Print full message. 
     print(userInfo) 
    } 

    private func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
//  let tokenChars = deviceToken.bytes 
     var tokenString = "" 

     for i in 0..<deviceToken.length { 
      tokenString += String(format: "%02.2hhx", arguments: [[deviceToken.bytes as! CVarArg][i]]) 
     } 

     FIRInstanceID.instanceID().setAPNSToken(deviceToken as Data, type: FIRInstanceIDAPNSTokenType.unknown) 

     print("tokenString: \(tokenString)") 
    } 
} 

Và đây là phần mở rộng ...

import Foundation 
import UserNotifications 
import FirebaseMessaging 

@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. 
     print("Message ID: \(userInfo["gcm.message_id"]!)") 

     // Print full message. 
     print("%@", userInfo) 

    } 

} 

extension AppDelegate : FIRMessagingDelegate { 
    // Receive data message on iOS 10 devices. 
    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 
     print("%@", remoteMessage.appData) 
    } 
} 

Token được đăng nhập vào giao diện điều khiển, FCM được kết nối, Cloud Messaging đang nắm giữ chứng chỉ của tôi ... gợi ý chỉ có thể tôi có thể đã là Bảng điều khiển Firebase không tính một thiết bị mà nó đang gửi một thư đến là 'Đã gửi'.

enter image description here Nhưng giao diện điều khiển có vẻ ổn.

2016-12-15 00:07:05.344 voltuser[4199:2008900] WARNING: Firebase Analytics App Delegate Proxy is disabled. To log deep link campaigns manually, call the methods in FIRAnalytics+AppDelegate.h. 
2016-12-15 00:07:05.349 voltuser[4199:2008900] Firebase automatic screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable automatic screen reporting, set the flag FirebaseAutomaticScreenReportingEnabled to NO in the Info.plist 
2016-12-15 00:07:05.447 voltuser[4199] <Debug> [Firebase/Core][I-COR000001] Configuring the default app. 
2016-12-15 00:07:05.514: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)" 
2016-12-15 00:07:05.520: <FIRMessaging/INFO> FIRMessaging library version 1.2.0 
2016-12-15 00:07:05.549 voltuser[4199:] <FIRAnalytics/INFO> Firebase Analytics v.3600000 started 
2016-12-15 00:07:05.549 voltuser[4199:] <FIRAnalytics/INFO> To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see this) 
2016-12-15 00:07:05.605 voltuser[4199] <Debug> [Firebase/Core][I-COR000018] Already sending logs. 
2016-12-15 00:07:05.679 voltuser[4199] <Debug> [Firebase/Core][I-COR000019] Clearcut post completed. 
2016-12-15 00:07:05.782 voltuser[4199] <Debug> [Firebase/Core][I-COR000019] Clearcut post completed. 
2016-12-15 00:07:05.824 voltuser[4199:] <FIRAnalytics/WARNING> The AdSupport Framework is not currently linked. Some features will not function properly. Learn more at here 
2016-12-15 00:07:07.286 voltuser[4199:] <FIRAnalytics/INFO> Firebase Analytics enabled 
InstanceID token: c_4iSvTQcHw:APA91bGjKnPoH9LysKl9CQxCJRJsfqwXBSUFAmgRp-KEWKjWqe2j4nt6Y5gx8us41rB6eLnRCOwRntnbr_N1fh_swz8j-GvvChSsV3gvG8dufVFLpagdtOxrxPSgLubQrfw-JqkA-4wV 
Connected to FCM. 
And the snapshot says Snap (mx7zr3y6XpSGZ4uB4PhZ8QRHIvt2) <null> 
[AnyHashable("notification"): { 
    body = "THIS IS SO PAINFUL"; 
    e = 1; 
    title = "WHY WONT YOU WORK"; 
}, AnyHashable("from"): 99570566728, AnyHashable("collapse_key"): com.mishastone.voltuser] 

... nhưng thư đang ghi lại tốt trong bảng điều khiển của tôi. Trong thực tế, không có lỗi outcrying ở tất cả!

phào nhẹ

Điện thoại của tôi không nhận được tiền cảnh, nền hoặc bất kỳ loại thông báo từ ứng dụng - không có gì cả. Zip. Zilch. Nada. Chỉ là tiếng đập vỡ của trái tim tôi.

iPhone của tôi hiện đang ở trên 9.3.5. Nếu điều đó giúp ích gì.

Bất kỳ trợ giúp nào sẽ được đánh giá cao - hoặc đề xuất cho các hệ thống thông báo đẩy thay thế ...

+0

Bạn có gửi thông báo từ Bảng điều khiển Firebase hoặc từ Máy chủ ứng dụng của mình không? Nếu từ Máy chủ ứng dụng, bạn có thể thêm vào trọng tải mẫu không? –

+0

Từ Bảng điều khiển Firebase. –

+0

may mắn với điều này? im hiện đang ở cùng một chiếc thuyền – CharlieNorris

Trả lời

10

Đã tìm ra. p.s. im sử dụng cú pháp Swift 3, bạn thiếu phương thức completionhandler trong phương thức willPresent của bạn

completionHandler Khối để thực thi với tùy chọn trình bày thông báo. Luôn thực hiện khối này tại một số thời điểm trong quá trình thực hiện phương pháp này.

https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter

func userNotificationCenter(_ center: UNUserNotificationCenter, 
          willPresent notification: UNNotification, 
          withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    let userInfo = notification.request.content.userInfo as! [String: Any] 



    completionHandler([.alert, .badge, .sound]) 

} 
+0

Ah, vậy đó là những gì nó đã được. Cuối cùng tôi đã sửa lỗi của mình bằng cách chỉ sao chép và dán mã ví dụ lặp đi lặp lại. Cảm ơn bạn đã chỉ ra các chi tiết cụ thể của nó! –

+0

Điều này có đúng không? OP cho biết "iPhone của tôi hiện đang ở trên 9.3.5", nhưng chức năng này (và tài liệu bạn đã liên kết) nói rằng nó dành cho SDK 10.0+ – tospig

+0

Bạn là người bảo vệ cuộc sống! Wow..thực sự cảm ơn – ken

0

Nếu bạn chắc chắn về mã của bạn là hoàn hảo và vẫn còn bạn không thể nhận được thông báo FCM sau đó vui lòng cập nhật chứng p12 từ giao diện điều khiển căn cứ hỏa lực. Bạn chỉ phải xuất khóa bảo mật từ Push certifcate và tải nó lên. Bạn sẽ bắt đầu nhận được thông báo.

Các vấn đề liên quan