В моем приложении я хочу включить/отключить push-уведомление со страницы настроек самого приложения. Может ли кто-нибудь предложить мне решение для включения/выключения статуса приложения в центре уведомлений из приложения?
Как включить/отключить push-уведомление из приложения?
Ответы (2)
вы можете зарегистрировать и отменить регистрацию удаленного уведомления с помощью приведенного ниже кода.
Зарегистрируйте 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)];
}
и отключите его с помощью кода ниже.
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
person
Paras Joshi
schedule
11.12.2012
давайте продолжим это обсуждение в чате
- person Jeff; 11.12.2012
@Muzammil - вот обновленный ответ до iOS 11 в Swift - stackoverflow.com/a/44672823/5638630
- person Krunal; 05.01.2018
Свифт 4
Включить push-уведомления (настройка из приложения):
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()
}
Делегируйте методы для обработки 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)
}
Отключить push-уведомления:
UIApplication.shared.unregisterForRemoteNotifications()
- Документ Apple по Push-уведомлениям
person
Krunal
schedule
21.06.2017