Hello guys, Today we learn about Enable/Disable Push Notification From The App functionality.
To get more knowledge about push notification functionality Please click Here.
In most of the apps, we give the push notification to enable/ disable the push notification feature from the device setting. but sometimes we want to give that feature inside our app setting. For this what do we do? so let’s start.
Getting Started:-
Step1:- Firstly Create an XCode project
File–> New–> Project–> iOS –> Next And Add your project name then create
Step2:- Secondly, Implement the push notification functionality for this, please click here.
step3:- After implementing the push notification, we give some notification enable disable button from where user easily turns on and off the notification working, when user clicks on the button it redirects to the iPhone device setting, to remove this functionality we need to add some cases in our code.
1 2 3 4 5 6 7 8 9 |
var isNotificationOn = true // on click of switch button isNotificationOn = !isNotificationOn let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate appDelegate?.setUpSettingPushNotification(isNotification: isNotificationOn) |
Here we need to call the app delegate class to operate push notification working when the user enables to disable the notification function inside the app
Now, we need to create a function in the AppDelegate class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
extension AppDelegate{ func setUpSettingPushNotification(isNotification: Bool){ if isNotification { if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self Messaging.messaging().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) self.application?.registerUserNotificationSettings(settings) } self.application?.registerForRemoteNotifications() }else{ self.application?.unregisterForRemoteNotifications() } } } |
Also, we need to call the same function in the didFinishLaunchingWithOptions method.
Conclusion:-
In, this blog we discussed the working of Enable/Disable Push Notification From The App.
I hope, this blog will help you to understand the flow of enabling/disabling functionality
Thanks for reading!!