Push Notification Enable and Disable by Using Switch

How do I enable and disable notifications using a switch?

of course it's not working outside of the AppDelegate, when your app first start, The code will execute only one time.
you can declare two methods on which contoller you want to switch the state.

-(void)switchOnRemoteNotification;
-(void)switchOffRemoteNotification;

when the switch turn on, call the switchOn... method, on the contrary, call the switchOff... method.

How to correctly disable/enable push notifications

According to docs for unregisterForRemoteNotifications:

You should call this method in rare circumstances only, such as when a
new version of the app removes support for all types of remote
notifications. Users can temporarily prevent apps from receiving
remote notifications through the Notifications section of the Settings
app. Apps unregistered through this method can always re-register.

The correct way (actually the way I have seen in the projects I have worked on) is to call an api and tell the backend that push notification should not be sent.

Using a switch to enable and disable local notifications (swift 3)

I suspect you're using the wrong cancel API.

Instead of:

UIApplication.shared.cancelAllLocalNotifications()

Try using:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

That cancels everything for your app though. If you want to do specific notifications, you can do:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "taskreminder")

More info can be seen at this closely related question.

2 )

Now in regards to your notifications not repeating, even though you've set true when setting up the trigger, I see that the time you're setting up requires a match for [.weekday, .hour, .minute], which means it'll repeat every day of the week at the same time.

Try passing THIS to UNNotificationRequest instead:

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)

Switch to enable/disable local notifications (swift 4)

you cannot change notification setting from the app but you can navigate your users to setting of your app so they can change notification settings

if let aString = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(aString, options: [:], completionHandler: { success in
SlideNavigationController.sharedInstance().closeMenu {

}
})
}

iOS - Enable/disable a group of notifications based on switch value

You can't interact with notifications that are sent while your app is not active, they will be received either way since they have been designed that way.

The fact that you can do that while the app is active is actually aimed at developers to allow them to use custom notification handling while the app is active instead of a method to filter them.

The best way to handle that is the one you proposed, the backend should filter what types of notifications to send to the user, and send only those to the user.

How to enable/disable push notification from my app

If a user denied permissions for push notifications you can not let him enable it from within the app.

You could however, set a button in your settings app (ViewController), and let the user switch the notifications off and on there. Then you can set a boolean to check before sending notifications. This way a user might use it instead of disabling the app's notification permission on the device settings.



Related Topics



Leave a reply



Submit