Nella mia app voglio abilitare/disabilitare la notifica push dalla pagina delle impostazioni della mia app stessa. Qualcuno mi suggerisce una soluzione per attivare/disattivare lo stato dell'app nel centro notifiche dall'app?Come abilitare/disabilitare la notifica push dall'app?
risposta
è 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];
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()
- Un documento da Apple su Push Notification
È swift 3 uguale? –
@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
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
: è possibile cambia il valore dello stile di avviso nel centro notifiche.Non rimane come 'Nessuno' – Jeff
@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 .. :) –