Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change push notifications programmatically in Swift

I have an app where I have implemented push notifications. I check with the user to allow remote notifications with:

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()

I also store the response in a DB with the device token.

I have also implemented a UISwitch in a settings page to enable/disable push notifications however I have only been able to enable/disable this in the DB column.

My problem is that if a user selected Don't Allow in the initial request, I cannot enable push notifications in the phone settings so even if I set the value to enable in the DB, the notification will never reach the phone as the phone settings are still set to disable.

Is there a way in Swift 2 to change the push notification in the phone setting from within the app instead of the user having to go in to the settings to change? Or is it completely redundant to have a UISwitch allow the user to toggle on/off push notifications?

like image 237
puks1978 Avatar asked Nov 04 '15 09:11

puks1978


3 Answers

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.

like image 193
Vishnu gondlekar Avatar answered Sep 29 '22 21:09

Vishnu gondlekar


Updated with swift 4 :

func switchChanged(sender: UISwitch!) {
    print("Switch value is \(sender.isOn)")

    if(sender.isOn){
        print("on")
        UIApplication.shared.registerForRemoteNotifications()
    }
    else{
        print("Off")
        UIApplication.shared.unregisterForRemoteNotifications()
    }
}
like image 35
Shivam Tripathi Avatar answered Sep 29 '22 22:09

Shivam Tripathi


if you are using FireBase to send push notifications to your devices , you can use topic subscription to enable push notification in subscribed devices and unsubscribe users from the topic when you don't want the user to receive push notification in those devices that have been unsubscribed.

to subscribe a user to a topic simply import Firebase, then use this method:

Messaging.messaging().subscribe(toTopic: "topicName")

and to unsubscribe a user use:

Messaging.messaging().unsubscribe(fromTopic: "topicName")
like image 35
Dyary Avatar answered Sep 29 '22 21:09

Dyary