Firebase Cloud Messaging sammple
我的app代表是
import UIKitimport Firebaseimport FirebaseMessagingimport UserNotificationsimport FirebaseInstanceID//add firebase code app delegate code@UIApplicationMainclass AppDelegate: UIResponder,UIApplicationDelegate {var window: UIWindow?let gcmMessageIDKey = "gcm.message_ID"func application(_ application: UIApplication,dIDFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // OverrIDe point for customization after application launch. // Register for remote notifications. This shows a permission dialog on first run,to // show the dialog at a more appropriate time move this registration accordingly. // [START register_for_notifications] 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,.sound],categorIEs: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications() // [END register_for_notifications] FirebaseApp.configure() // [START set_messaging_delegate] Messaging.messaging().delegate = self // [END set_messaging_delegate] return true}// [START receive_message]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)}func application(_ application: UIApplication,dIDReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaPing (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. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: \(messageID)") } // Print full message. print(userInfo) completionHandler(UIBackgroundFetchResult.newData)}// [END receive_message]func application(_ application: UIApplication,dIDFailToRegisterForRemoteNotificationsWithError error: Error) { print("Unable to register for remote notifications: \(error.localizedDescription)")}// This function is added here only for deBUGging purposes,and can be removed if swizzling is enabled.// If swizzling is Disabled then this function must be implemented so that the APNs token can be paired to// the InstanceID token.func application(_ application: UIApplication,dIDRegisterForRemoteNotificationsWithDevicetoken devicetoken: Data) { print("APNs token retrIEved: \(devicetoken)") // With swizzling Disabled you must set the APNs token here. InstanceID.instanceID().setAPNSToken(devicetoken,type: InstanceIDAPNSTokenType.sandBox)}} // [START ios_10_message_handling] @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. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: \(messageID)") } // Print full message. print(userInfo) // Change this to your preferred presentation option completionHandler([.alert,.sound])}func userNotificationCenter(_ center: UNUserNotificationCenter,dIDReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaPing () -> VoID) { let userInfo = response.notification.request.content.userInfo // Print message ID. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: \(messageID)") } // Print full message. print(userInfo) completionHandler()}}// [END ios_10_message_handling]extension AppDelegate : MessagingDelegate {// [START refresh_token]func messaging(_ messaging: Messaging,dIDRefreshRegistrationToken fcmToken: String) { print("Firebase registration token: \(fcmToken)") print(fcmToken) resgisterNotificationToken(fcmToken: fcmToken)}// [END refresh_token]func application(received remoteMessage: MessagingRemoteMessage) { //get called when sending notification from POSTMAN and when app is open print("%@",remoteMessage.appData) print("%@",remoteMessage)}func resgisterNotificationToken(fcmToken:String){ //let deviceid = UIDevice.current.IDentifIErForvendor!.uuIDString //let parameters = ["OTY": AppConstants.init().OS_TYPE,"REGID": fcmToken] as Dictionary<String,String>}}
我可以收到从firebase控制台发送的通知.
我已将我的firebase库升级到最新的3.0版本.
我也得到了警告.
不推荐使用“InstanceIDAPNSTokenType”:改为使用FIRMessaging的APNSToken属性.
请提供代码解决方案,并给我服务器请求结构,以便我可以从邮递员测试它.
提前致谢.
解决方法 您可以尝试像这样设置apn令牌:FIRInstanceID.instanceID() .setAPNSToken(devicetoken,type: FIRInstanceIDAPNSTokenType.unkNown)
FIRInstanceID setAPNSToken
Set APNS token for the application. This APNS token will be used to register with Firebase Messaging using token or tokenWithAuthorizedEntity:scope:options:handler. If the token type is set to FIRInstanceIDAPNSTokenTypeUnkNown InstanceID will read the provisioning profile to find out the token type.
Firebase API reference
它对我有用!
编辑:
使用Firebase 4.0.0版本的方法已经改变:
Messaging.messaging() .setAPNSToken(devicetoken,type: MessagingAPNSTokenType.unkNown)
FIRMessaging API reference
总结以上是内存溢出为你收集整理的我无法接收iOS swift 3的数据通知全部内容,希望文章能够帮你解决我无法接收iOS swift 3的数据通知所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)