2016-09-30 16 views
20

में पुश नोटिफिकेशन प्राप्त करने में समस्या आईओएस 10 के लिए पुश अधिसूचना सेट अप करने का प्रयास कर रहा है।आईओएस 10 और आईओएस 9

// Register for the defined notifications 

     if #available(iOS 10.0, *) { 

      let center = UNUserNotificationCenter.current() 
      center.delegate = UIApplication.shared.delegate as! AppDelegate 
      center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in 

       if error == nil { 
        UIApplication.shared.registerForRemoteNotifications() 
       } 
      } 

     } else { 

      // Fallback on earlier versions 

      let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: [autoCancelCategory]) 
      UIApplication.shared.registerUserNotificationSettings(notificationSettings) 
      UIApplication.shared.registerForRemoteNotifications() 

     } 

यह मेरे विचार नियंत्रकों में से एक में लॉगिन^पर कहा जाता है: यह कुछ गाइड मेरी सेटअप इसलिए की तरह है के माध्यम से नीचे से 10
पढ़ें संस्करणों के लिए पहले से काम कर रहा था।

और अब AppDelegate में, यह UNUserNotificationCenterDelegate के अनुरूप है, मैं इन तरीकों है:

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    print("Got a push!") 
} 


@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    print("Got a push!") 
} 

और मैं भी है:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 

    let settings = UserDefaults.standard 
    settings.setValue(deviceToken, forKey: "deviceToken") 
} 

डिवाइस टोकन Data के रूप में सर्वर पर अपलोड है, और इसने पिछले संस्करणों के लिए ठीक काम किया है।
मैंने जांच की है कि सर्वर पर टोकन फोन पर एक से मेल खाता है।
मैंने Capabilities में सभी लक्ष्यों के लिए पुश सूचनाएं सक्षम की हैं, मैंने Add the push Notifications entitlement to your entitlements file भी चुना है।
धक्का पर हालांकि कुछ भी प्राप्त नहीं कर रहा है।
कोई विचार क्या मैं यहां गलत कर सकता हूं? किसी भी संकेतक वास्तव में सराहना की जाएगी!

धन्यवाद!

संपादित करें: मैंने यह भी देखा है कि पुश सूचनाएं आईओएस 9 में काम नहीं कर रही हैं।

+0

यह ओएस समस्या हो सकती है। कृपया डिवाइस को पुनरारंभ करके आज़माएं। https://twitter.com/yogye7/status/781340933228945408 –

उत्तर

1

इसका उत्तर देने के लिए, शायद यह अन्य के लिए उपयोगी होगा। मेरा टोकन गलत था। हालिया अपडेट के साथ एन्कोडिंग बदल गई है। मैंने वास्तव में इसके लिए सभी स्टैक ओवरफ़्लो खोजे थे और यह मेरी समझ थी कि base64string का उपयोग करके मुझे Data को String में परिवर्तित करना पड़ा। हालांकि मैंने देखा कि इस स्ट्रिंग में कुछ विषम वर्ण थे जैसे कि \

extension Data { 

    func hexString() -> String { 
     return self.reduce("") { string, byte in 
      string + String(format: "%02X", byte) 
     } 
    } 

} 

यह सही स्वरूप को Data धर्मान्तरित: मैं अंत में एक Data विस्तार कर रही है समाप्त हो गया। तो अगर यह आपके रूपांतरण करना है, के साथ साथ सभी हकों सेट कर रहे हैं, तो आप नए तरीकों को जोड़ने के लिए याद रखें:

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    print("Got a push!") 
} 


@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    print("Got a push!") 
} 

और सेट didFinishLaunchingWithOptions हर चीज में प्रतिनिधि वस्तु जाना अच्छा होना चाहिए।

2

क्या आपने जांच की है कि बिल्ड सेटिंग्स> कोड साइनिंग एंटाइटेलमेंट्स के पास एंटाइटेलमेंट फ़ाइल का संदर्भ है? [AppName] .entitlements।

और हकों फ़ाइल सही जानकारी शामिल है कि:

(कुंजी: ए पी एस पर्यावरण, मूल्य: विकास)

उसके अलावा, आप पुश प्रमाण पत्र पुनः कोशिश कर सकते हैं। आईओएस 10 अपग्रेड (लेकिन सैंडबॉक्स पर्यावरण में नहीं) के बाद उत्पादन में काम करने के लिए हमें नए प्रमाणपत्र बनाना पड़ा।

+0

बिल्ड सेटिंग्स> कोड साइनिंग एंटाइटेलमेंट की जांच की गई और वहां कोई समस्या नहीं प्रतीत होती है। – Kex

+0

@ केक्स को हमारी कंपनी में ऐप्स की संख्या के साथ एक ही समस्या थी। कृपया बिल्ड सेटिंग्स> क्षमताओं को भी जांचें और सुनिश्चित करें कि "नोटिफिकेशन" चालू हैं। – ibnetariq

2

बस ध्यान दिया गया है कि आप application(_:willFinishLaunchingWithOptions:) या application(_:didFinishLaunchingWithOptions:) विधियों के बाद प्रतिनिधि को पंजीकृत कर रहे हैं।

शायद यही कारण है कि आपके प्रतिनिधि तरीकों को हिट नहीं किया जा रहा है?

docs देखें:

महत्वपूर्ण

आप कोई बाद में अपने एप्लिकेशन को लॉन्च करना समाप्त करने से पहले UNUserNotificationCenter वस्तु के लिए अपने प्रतिनिधि वस्तु सौंपनी होगी। उदाहरण के लिए, एक आईओएस ऐप में, आपको इसे एप्लिकेशन (: willFinishLaunchingWithOptions :) या एप्लिकेशन (: didFinishLaunchingWithOptions :) विधि में असाइन करना होगा।

+0

वास्तव में अच्छी जगह! जब मैं घर वापस आऊंगा और आपको परिणाम बता दूंगा तो मैं इसे एक शॉट दूंगा। – Kex

+0

चले गए 'चलो केंद्र = UNUserNotificationCenter.current(); center.delegate = self' to func application (_ application: UIAplplication, didFinishLaunchingWithOptions launchOptions: [UIAplplicationLaunchOptionsKey: Any]?) -> बूल' और अभी भी – Kex

+0

के माध्यम से कुछ भी नहीं आ रहा है मुझे यह मिला है: 'if # उपलब्ध (आईओएस 10.0, *) { UNUserNotificationCenter.currentNotificationCenter()। प्रतिनिधि = स्वयं } ' – Mark