0

Ich verwende den Push-Benachrichtigungsdienst für meine Demo-App. Mit Firebase kann ich die Benachrichtigungen an das Gerät senden. Aber einige Warnungen kommen in meiner Log-Nachricht. Wie kann ich es entfernen? Und eine andere Sache, wie kann ich das Gerät Token hier mit der aktualisierten Xcode 8 in Swift 3.Eine Warnung in Swift 3 erhalten, während Push Notification mit Firebase verwendet wird?

screenshots

AppDelegate Codes erhalten:

import UIKit 
    import Firebase 
    import UserNotifications 
    import FirebaseInstanceID 
    import FirebaseMessaging 

    @UIApplicationMain 
    class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 



    UINavigationBar.appearance().barTintColor = UIColor(red: 233.0/255.0, green: 86.0/255.0, blue: 53.0/255.0, alpha: 1.0) 

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()] 
    UIBarButtonItem.appearance().tintColor = UIColor.whiteColor() 


    UITabBar.appearance().backgroundColor = UIColor.whiteColor() 
    // UITabBar.appearance().tintColor = UIColor.orangeColor() 
    UITabBar.appearance().tintColor = UIColor(red: 233.0/255.0, green: 86.0/255.0, blue: 53.0/255.0, alpha: 1.0) 

    // [START register_for_notifications] 
    if #available(iOS 10.0, *) { 
     let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound] 
     UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
      authOptions, 
      completionHandler: {_,_ 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() 

    // [END register_for_notifications] 

    // Use Firebase library to configure APIs 
    FIRApp.configure() 

    // Add observer for InstanceID token refresh callback. 
    NSNotificationCenter.defaultCenter().addObserver(self, 
                selector: #selector(self.tokenRefreshNotification), 
                name: kFIRInstanceIDTokenRefreshNotification, 
                object: nil) 

    return true 
} 

// [START receive_message] 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 

    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

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

} 
// [END receive_message] 

// [START refresh_token] 
func tokenRefreshNotification(notification: NSNotification) { 
    if let refreshedToken = FIRInstanceID.instanceID().token() { 
     print("InstanceID token: \(refreshedToken)") 
     print("InstanceID token: \(notification.object)") 
    } 
    // Connect to FCM since connection may have failed when attempted before having a token. 
    connectToFcm() 
} 
// [END refresh_token] 


func application(application: UIApplication, 
       didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox) 


} 

// [START connect_to_fcm] 
func connectToFcm() { 
    FIRMessaging.messaging().connectWithCompletion { (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) { 

} 

// [START disconnect_from_fcm] 
func applicationDidEnterBackground(application: UIApplication) { 
    FIRMessaging.messaging().disconnect() 
    print("Disconnected from FCM.") 

} 
// [END disconnect_from_fcm] 

func applicationWillEnterForeground(application: UIApplication) { 
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
} 

func applicationDidBecomeActive(application: UIApplication) { 

connectToFcm() 

} 

func applicationWillTerminate(application: UIApplication) { 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 


} 

// [START ios_10_message_handling] 
    @available(iOS 10, *) 
extension AppDelegate : UNUserNotificationCenterDelegate { 

// Receive displayed notifications for iOS 10 devices. 
func userNotificationCenter(center: UNUserNotificationCenter, 
          willPresentNotification notification: UNNotification, 
                withCompletionHandler completionHandler: (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) 
// } 
//} 
+0

Hier sehen meine ans => http://stackoverflow.com/questions/37438369/apns-firebase-notification-failed-to-fetch-token/ 38351485 # 38351485 –

+0

Ich habe versucht, diesen Link aber ia bekommen Fehler in dieser Zeile "#selector (tokenRefreshNotification (_ :)) @SanandiyaVipul –

+0

Zuerst lesen Sie meine ans und fügen Sie diese Methode => tokenRefreshNotification –

Antwort

1
  1. erster dieses Observer hinzufügen in didFinishLaunchingWithOptions
NSNotificationCenter.defaultCenter().addObserver(self, 
                selector: #selector(tokenRefreshNotification(_:)), 
                name: kFIRInstanceIDTokenRefreshNotification, 
                object: nil) 
  1. Dann diese Add-Methode in Appdelegate.M Klasse
func tokenRefreshNotification(notification: NSNotification) { 

    let refreshedToken = FIRInstanceID.instanceID().token() 
    print("InstanceID token: \(refreshedToken)") 
    print("InstanceID token: \(notification.Object)") 


    connectToFcm() 
} 
  1. prüfen Sie, ob diese Methode (tokenRefreshNotification) Wird aufgerufen oder nicht?

Bei dieser Methode u erhalten Token ID

+0

Nein, es ruft nicht "tokenRefreshNotification". –

+0

Überprüfen Sie dies wieder => http://StackOverflow.com/questions/37667753/ios-firebase-push-notifications-how-to-give-firebase-users-device-token-and-s/37789322#37789322 –