2016-05-19 11 views
19

मेरे पास एक ऐसा एप्लिकेशन है जो उपयोगकर्ता के सत्र को NSUserDefaults में संग्रहीत करता है।क्लाउड मैसेजिंग हैंडिंग टर्मिनेट ऐप

override func viewWillAppear(animated: Bool) { 

    self.view.hidden = true 

    let defaults = NSUserDefaults.standardUserDefaults() 
    if defaults.stringForKey("user") != nil 
    { 

     dispatch_async(dispatch_get_main_queue(), {() -> Void in 
      let viewController:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("vistaInicio") as! ViewControllerInicio 
      self.presentViewController(viewController, animated: true, completion: nil) 
     }) 


    }else 
    { 
    self.view.hidden = false 

    } 

} 

यह सुचारू रूप से मुझे आज तक काम किया जब मैं करने का फैसला किया: जब अनुप्रयोग को बंद करने के लिए मजबूर किया जाता है, प्रारंभिक में है कि क्या डेटा नियंत्रक उपयोगकर्ता सत्र वहाँ अगर वहाँ यह शुरुआत खिड़की के लिए भेजा के रूप में इस की पुष्टि, मामले में इस ट्यूटोरियल Setting up a Firebase Cloud Messaging Client App on iOS के बाद फायरबेस अपडेट करने के साथ पुश सूचनाएं लागू करें। समस्या तब होती है जब वह आवेदन को मार डाला और प्रवेश फिर निम्न त्रुटि कोड देता है:,

2016-05-19 16:05:27.647: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(full)" 
2016-05-19 16:05:27.659: <FIRMessaging/INFO> FIRMessaging library version 1.1.0 
2016-05-19 16:05:27.831: <FIRMessaging/WARNING> FIRMessaging registration is not ready with auth credentials 
Unable to connect with FCM. Optional(Error Domain=com.google.fcm Code=501 "(null)") 

Screenshot

+0

क्या आपने सामान्य फ़ायरबेस फ्रेमवर्क को सही ढंग से प्रारंभ किया है? –

+0

हां, मेरा ऐपडिलेगेट इस उदाहरण के समान है [फायरबेस मैसेजिंग क्विकस्टार्ट] (https://github.com/firebase/quickstart-ios/tree/master/messaging)। मुझे अधिसूचनाएं सही तरीके से प्राप्त होती हैं, समस्या तब होती है जब मैं एप्लिकेशन को मारता हूं और क्रैश को फिर से खोलता है, फिर सामान्य रूप से पुनः प्रयास करें और ऑपरेटिंग करें। –

उत्तर

34

यहाँ समाधान है

पहले अपलोड Firebase कंसोल में आवश्यक प्रमाण पत्र फिर अपने अनुप्रयोग में पुश नोटिफिकेशन और पृष्ठभूमि मोड सक्षम करें -> रिमोट नोटिफिकेशन

इसके बाद ऐप प्रतिनिधि में नीचे दिए गए कोड का उपयोग करें (मैं मुश्किल रेखा निर्दिष्ट करता हूं):

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    registerForPushNotifications(application) 
    // Override point for customization after application launch. 
    // Use Firebase library to configure APIs 
    FIRApp.configure() 
    return true 
} 

func registerForPushNotifications(application: UIApplication) { 
    let notificationSettings = UIUserNotificationSettings(
     forTypes: [.Badge, .Sound, .Alert], categories: nil) 
    application.registerUserNotificationSettings(notificationSettings) 
} 

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) { 
    if notificationSettings.types != .None { 
     application.registerForRemoteNotifications() 
    } 
} 

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes) 
    var tokenString = "" 

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

    //Tricky line 
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown) 
    print("Device Token:", tokenString) 
} 
+0

यह समस्या हल करता है, अब त्रुटि नहीं देता है। अब जब मैं पृष्ठभूमि में हूं तो डेटा के डाउनलोड को नियंत्रित कर सकता हूं? –

+0

यह मेरे लिए भी हल किया! धन्यवाद! – Jorsh

+3

आपके पास इस कोड का उद्देश्य-सी संस्करण है? –

5

AppDelegate में भूल न करें:

import Firebase 
import FirebaseInstanceID 
import FirebaseMessaging 
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    registerForPushNotifications(application) 
    FIRApp.configure() 

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

    // Override point for customization after application launch. 
    return true 
    } 

func registerForPushNotifications(application: UIApplication) { 
     let settings: UIUserNotificationSettings = 
     UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
     application.registerForRemoteNotifications() 
    } 


    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
        fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    print("===== didReceiveRemoteNotification ===== %@", userInfo) 
    } 


func tokenRefreshNotificaiton(notification: NSNotification) { 
    let refreshedToken = FIRInstanceID.instanceID().token()! 
    print("InstanceID token: \(refreshedToken)") 

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

    func connectToFcm() { 
    FIRMessaging.messaging().connectWithCompletion { (error) in 
     if (error != nil) { 
     print("Unable to connect with FCM. \(error)") 
     } else { 
     print("Connected to FCM.") 
     } 
    } 
    } 

कृपया यह भी सुनिश्चित करें कि Info.plist में करने के लिए करते हैं: FirebaseAppDelegateProxyEnabled = NO

मैं अब के लिए पता नहीं है, लेकिन मैं में print(...) मिला didReceiveRemoteNotification लेकिन पॉपअप प्राप्त नहीं करें। > कंसोल - -> अधिसूचना - मैं से Firebase संदेश भेजने> एकल डिवाइस और यहाँ नकल टोकन जो मैं Xcode कंसोल से मिला ->func tokenRefreshNotificaiton

+0

गायब टुकड़ा: '.addObserver', मुझे बचाया! –

+0

@ श्री बिस्टा। कृपया मुझे पुश अधिसूचना के बारे में मार्गदर्शन करें, एफसीएम के साथ काम करना .मैं सर्वर से डेटा संदेश करने में सक्षम हूं लेकिन अधिसूचना विधि कॉल नहीं कर रही है। कृपया मेरा प्रश्न http: // stackoverflow जांचें।कॉम/प्रश्न/411 9 8651/एक्सकोड -8-1-पुश-नोटिफिकेशन-इन-स्विफ्ट-2-3-फ़ायरबेस-एकीकरण-नहीं-प्राप्त हो रहा है –

4

जवाब सही है उन कदमों कर रहे हैं, लेकिन यह भी अपने डिवाइस की जाँच करें पहर । चूंकि आपका समय और तिथि बहुत दूर है, तो यह काम नहीं करेगा

2

ट्रिपल को पूरे एकीकरण की जांच करने के बाद, मेरे लिए यह मुद्दा था कि परीक्षण डिवाइस की तारीख एक सप्ताह पहले बदल गई थी। शायद एफसीएम एसडीके कुछ दिनांक आधारित चेक करता है।

त्रुटि फ़ायरबेस पक्ष पर कम सामान्य हो सकती है, क्योंकि मैं लगभग एक दिन समाधान खोजने के लिए खो गया था। उम्मीद है की यह मदद करेगा।

+0

आपको बहुत धन्यवाद, यह वास्तव में मेरी समस्या भी थी! – manisha

संबंधित मुद्दे