Change Push Notifications Programmatically in Swift

Change push notifications programmatically in Swift

There isn't any way you can change push notifications permission status from program. Also, prompt asking user to allow push notifications can not be shown again and again. You can refer this https://developer.apple.com/library/ios/technotes/tn2265/_index.html.

The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

So using UISwitch to toggle permission status doesn't make any sense unless you use switch status to turn on/off remote notifications from your server.

Disable push notifications programmatically

I figured it out thanks to @wsnjy.

The following code disables notifications:

UIApplication.shared.unregisterForRemoteNotifications()

Enable or Disable Iphone Push Notifications inside the app

First thing is that you can not enable and disable push notification in inside the app. If you have found some apps which did it than there must be workaround solution.

Like if you want to do Inside the app then use one identifier and send it to server according push notification enable and disable button. So, your server side coding use this identifier and work according to that. Like identifier is say it's enable than your server will send notification otherwise not.

You can check that user set enable or disable Push Notifications using following code.

Enable or Disable Iphone Push Notifications

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
// Yes it is..

Hope, this will help you..

Swift -How to change push notification && sound from within the app itself?

There doesn't seem a possible way to do this from within the app itself

https://stackoverflow.com/a/33520827/4833705

https://stackoverflow.com/a/36654919/4833705

Also I tried the code below and when Notifications were on (from outside the app) I used this to toggle them off and it didn't do anything.

@objc func switchValueDidChange(_ sender: UISwitch) {
if (sender.isOn == true) {

// *** doesn't work ***
UIApplication.shared.registerForRemoteNotifications()

} else {

// *** doesn't work ***
UIApplication.shared.unregisterForRemoteNotifications()
}
}

It's possible to change push notification message before display on device from iOS side?

There are various possibilities for preprocessing and modify the payload of a notification on iOS.

Before iOS 10

You could be sending Silent Notifications, what will not be not shown to the user. Will wake our application when it is terminated or in background, and you will be able to do preprocessing on the notification content. See more info here, how to set it up. However, this notification type is not 100% reliable, and should not be abused, e.g. used for all notifications to be delivered, because Apple could stop the notifications to be sent after a number of messages.

The other option is, if your application supports VoIP. This way your app will pretty much always listen to push notifications, and you will be always available to preprocess the notifications, before you would be displaying them. However, if your app does not have real VoIP capabilities, e.g. phone calls, your app will be rejected by Apple on the review. Here is a great tutorial, how to set it up.

From iOS 10

With the introduction of iOS 10, we are finally able to do preprocessing on our notifications, even, when the app is in background or terminated. No VoIP capabilities or special type of notifications needed to be sent. Here is a great tutorial, how to set it up.

How to enable push notification from app settings in iOS Swift 5?

Once a user has denied the permissions for push notifications, he has to enable them from within the settings app in order to get push notifications. So on your settings screen, when user taps on enable notification option just take him to the notifications settings screen of your app. And from there he can enable it. Use this piece of code for opening settings app.

if let bundle = Bundle.main.bundleIdentifier,
let settings = URL(string: UIApplication.openSettingsURLString + bundle) {
if UIApplication.shared.canOpenURL(settings) {
UIApplication.shared.open(settings)
}
}

After enabling from within the settings app, status won't be denied anymore.



Related Topics



Leave a reply



Submit