19

मेरे ऐप में मैं अपने ऐप के सेटिंग पेज से पुश अधिसूचना को सक्षम/अक्षम करना चाहता हूं। क्या कोई मुझे ऐप से अधिसूचना केंद्र में ऐप की स्थिति चालू/बंद करने का समाधान सुझा सकता है?ऐप से पुश अधिसूचना को सक्षम/अक्षम कैसे करें?

उत्तर

54

आप पंजीकरण कोड के साथ रिमोट अधिसूचना पंजीकृत और पंजीकरण कर सकते हैं।

रजिस्टर bellow code..means साथ RemoteNotification अधिसूचना

//-- Set Notification 
if ([[UIApplication sharedApplication]respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
{ 
    // For iOS 8 and above 
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 

    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
} 
else 
{ 
    // For iOS < 8 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
    (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 
} 

सक्षम और bellow कोड के साथ अक्षम करें।

[[UIApplication sharedApplication] unregisterForRemoteNotifications]; 
+0

Thanx दोस्त पर एप्पल द्वारा एक दस्तावेज़ .. इसके Appdelegate.But में प्रतिनिधि में प्रवेश इसकी सूचना केंद्र – Jeff

+0

में चेतावनी शैली के मूल्य में बदलाव नहीं: यह संभव है अधिसूचना केंद्र में अलर्ट शैली के मूल्य को बदलें। यह 'कोई नहीं' – Jeff

+0

@Jeff के लिए इस उत्तर दोस्त को देखता है http://stackoverflow.com/questions/1535403/determine-on-iphone-if-user-has- सक्षम-पुश-नोटिफिकेशन हो सकता है कि आप इससे विचार प्राप्त कर सकें .. :) –

4

स्विफ्ट 4

पुश नोटिफिकेशन (app से सेटअप) सक्षम करें:

:

if #available(iOS 10.0, *) { 

    // SETUP FOR NOTIFICATION FOR iOS >= 10.0 
    let center = UNUserNotificationCenter.current() 
    center.delegate = self 
    center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in 
     if error == nil{ 
      DispatchQueue.main.async(execute: { 
       UIApplication.shared.registerForRemoteNotifications() 
      }) 
     } 
    } 

} else { 

    // SETUP FOR NOTIFICATION FOR iOS < 10.0 

    let settings = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil) 
    UIApplication.shared.registerUserNotificationSettings(settings) 

    // This is an asynchronous method to retrieve a Device Token 
    // Callbacks are in AppDelegate.swift 
    // Success = didRegisterForRemoteNotificationsWithDeviceToken 
    // Fail = didFailToRegisterForRemoteNotificationsWithError 
    UIApplication.shared.registerForRemoteNotifications() 
} 

प्रतिनिधि सूचनाएं

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

} 


func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    // ...register device token with our Time Entry API server via REST 
} 


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 
    //print("DidFaildRegistration : Device token for push notifications: FAIL -- ") 
    //print(error.localizedDescription) 
} 

पुश Notifiacation अक्षम धक्का संभाल करने के तरीकों

UIApplication.shared.unregisterForRemoteNotifications() 
+0

स्विफ्ट 3 समान है? –

+0

@ जॉनीडगुड मुझे स्विफ्ट 3 के बारे में निश्चित नहीं है क्योंकि मैंने स्विफ्ट 4 के समर्थन के साथ एक्सकोड 9 बीटा के साथ अपनी परियोजना में इसे कोड किया है और स्विफ्ट 4 के साथ काम कर रहा है। लेकिन हां, यह पुश अधिसूचना कोडिंग पैटर्न में बड़ा अंतर नहीं हो सकता है तेज़ 3 और 4. मैं इस कोड के साथ स्विफ्ट 3 में कोशिश करूंगा और यहां अपडेट करूँगा। – Krunal

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