2017-02-09 7 views
6

ऐप को अग्रभूमि में और स्विफ्ट 3 और आईओएस 10 के साथ पृष्ठभूमि में होने पर पुश नोटिफिकेशन को संभालने के लिए मैं अपना ऐपडिलेगेट कैसे सेट करूं? अगर मुझे नोटिफिकेशन मिलता है तो अग्रभूमि में फोन को कंपन करने के तरीके को शामिल करना शामिल है।ऐप अग्रभूमि में होने पर आईओएस पुश सूचनाओं को कैसे संभाल सकता हूं?

पुश सूचनाएं संभाल करने के लिए निम्न ढांचे आयात:

import UserNotifications 

किसी भी उपकरण के आयात निम्न ढांचे पर फ़ोन कंपन करने के लिए

उत्तर

7

यहाँ कैसे मैं अपने AppDelegate यह करने के लिए फ़ाइल की स्थापना की है :

import AudioToolbox 

करें कि आपके AppDelegate एक UNUserNotificationCenterDelegate:

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 

अपने "didFinishLaunchingWithOptions" में इस जोड़ें:

UNUserNotificationCenter.current().delegate = self 
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in 
     if (granted) { 
      UIApplication.shared.registerForRemoteNotifications() 
     } else{ 
      print("Notification permissions not granted") 
     } 
    }) 

यह यदि उपयोगकर्ता पहले से कहा है कि अपने app सूचनाएं भेज सकते हैं का निर्धारण करेगा। यदि नहीं, तो इसे कैसे संभालें इसे संभालें।

एक बार यह पंजीकृत है डिवाइस टोकन तक पहुँच प्राप्त करने के लिए:

//Completed registering for notifications. Store the device token to be saved later 
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 

    self.deviceTokenString = deviceToken.hexString 
} 

hexString एक विस्तार मैं अपने प्रोजेक्ट में जोड़ी है:

extension Data { 
    var hexString: String { 
     return map { String(format: "%02.2hhx", arguments: [$0]) }.joined() 
    } 
} 

को संभालने के लिए क्या होता है जब आपके एप्लिकेशन को वह सूचना प्राप्त करता है अग्रभूमि में:

//Called when a notification is delivered to a foreground app. 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    //Handle the notification 
    //This will get the text sent in your notification 
    let body = notification.request.content.body 

    //This works for iphone 7 and above using haptic feedback 
    let feedbackGenerator = UINotificationFeedbackGenerator() 
    feedbackGenerator.notificationOccurred(.success) 

    //This works for all devices. Choose one or the other. 
    AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate), nil) 
} 

आपके ऐप को प्राप्त होने पर क्या होता है इसे संभालने के लिए पृष्ठभूमि में अधिसूचना, जिसके बाद उपयोगकर्ता अधिसूचना पर दबाता है:

//Called when a notification is delivered to a background app. 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    //Handle the notification 
    print("did receive") 
    let body = response.notification.request.content.body 
    completionHandler() 

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

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