Odpowiedz

54

można zarejestrować i wyrejestrować zdalne powiadomienie z kodem mieszkowym.

Rejestracja RemoteNotification z mieszkiem code..means Włącz powiadomienie

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

i wyłączyć go z kodem poniżej.

[[UIApplication sharedApplication] unregisterForRemoteNotifications]; 
+0

THANX kolesia .. Jej zawarcie delegata w Appdelegate.But ITS nie zmieniając wartość alertu stylu w centrum powiadomień – Jeff

+0

: czy jest to możliwe zmień wartość stylu alertu w centrum powiadomień.Nie pozostaje jako "Brak" – Jeff

+0

@Jeff dla tego zobacz tę odpowiedź koleś http://stackoverflow.com/questions/1535403/determine-on-iphone-if-user-has- Włączone powiadomienia push mogą być na tym etapie .. :) –

4

Swift 4

Włącz powiadomienia push (Setup od aplikacji):

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

metody Delegowanie do obsługi powiadomień 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) 
} 

Wyłącz push Notifiacation:

UIApplication.shared.unregisterForRemoteNotifications() 
+0

Czy to samo jest szybkie? –

+0

@JohnyDGood Nie jestem pewien na temat Swift 3, ponieważ zakodowałem to w moim projekcie z Xcode 9 beta z obsługą szybkiego 4 i działa dobrze z szybkim 4. Ale tak, nie może to być główna różnica w stylu kodowania powiadomień Push w swift 3 i 4. Spróbuję w Swift 3 z tym kodem i zaktualizuję go tutaj. – Krunal

Powiązane problemy