risposta

54

è possibile registrare e annullare la registrazione della notifica remota con il codice qui sotto.

Registrati RemoteNotification con code..means soffietto abilitare la notifica

//-- 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)]; 
} 

e disattivarla con il codice di soffietto.

[[UIApplication sharedApplication] unregisterForRemoteNotifications]; 
+0

Grazie tizio .. La sua stipula del delegato in Appdelegate.But la sua non è la modifica del valore dello stile allarme nel centro di notifica – Jeff

+0

: è possibile cambia il valore dello stile di avviso nel centro notifiche.Non rimane come 'Nessuno' – Jeff

+0

@Jeff per questo vedi questo answer amico http://stackoverflow.com/questions/1535403/determine-on-iphone-if-user-has- è possibile ottenere le notifiche push attivate da questo .. :) –

4

Swift 4

Abilita push di notifica (programma di installazione da 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() 
} 

metodi delegato per gestire le notifiche push

@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) 
} 

Disabilita spinta Notifiacation:

UIApplication.shared.unregisterForRemoteNotifications() 
+0

È swift 3 uguale? –

+0

@JohnyDGood Non sono sicuro di swift 3 come ho codificato nel mio progetto con Xcode 9 beta con supporto di swift 4 e funziona bene con swift 4. Ma sì, potrebbe non esserci una grande differenza nel modello di codifica di notifica Push in swift 3 e 4. Proverò in Swift 3 con questo codice e aggiornerò qui. – Krunal