3

So I have push notifications implemented in my App and when the app first starts up, its asks users if they would like to allow push notifications (this implementation works fine as expected).

If this user disallows the push notifications, is it possible to have a button in the app which allows the user to click on and it would ask to allow permissions again?

This is what im trying to achieve:

SettingsView

//IF PUSH NOTIFICATIONS NOT ENABLED, SHOW THIS SECTION

Section (header: Text("Push Notifications")) {
                        HStack {
                            Image(systemName: "folder")
                                .resizable()
                                .frame(width: 20, height: 20)
                            VStack(alignment: .leading) {
                                Text("Enable Push Notifications").font(.callout).fontWeight(.medium)
                            }
                            Spacer()
                            Button(action: {
                                checkPushNotifications()
                            }) {
                                Text("View").font(.system(size:12))
                            }
                        }
                    }

In my Push Notification Function:

class PushNotificationService: NSObject, MessagingDelegate {
    
    static let shared = PushNotificationService()
    private let SERVER_KEY = "myserverkey"
    private let NOTIFICATION_URL = URL(string: "https://fcm.googleapis.com/fcm/send")!
    private let PROJECT_ID = "my project name"
    
    private override init() {
        super.init()
        Messaging.messaging().delegate = self
    }
    
    func askForPermission() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted: Bool, error: Error?) in
            if granted {
                self.refreshFCMToken()
            } else {
                // Maybe tell the user to go to settings later and re-enable push notifications
            }
        }
    }
    
    func refreshFCMToken() {
        InstanceID.instanceID().instanceID { (result, error) in
            if let error = error {
                print("Error fetching remote instance ID: \(error)")
            } else if let result = result {
                print("Remote instance ID token: \(result.token)")
                self.updateFCMToken(result.token)
            }
        }
    }
    
    func updateFCMToken(_ token: String) {
        guard let currentUser = Auth.auth().currentUser else { return }
        let firestoreUserDocumentReference = Firestore.firestore().collection("users").document(currentUser.uid)
        firestoreUserDocumentReference.updateData([
            "fcmToken" : token
        ])
    }
}

What im trying to achieve is if the user HAS NOT enabled notification only then ask them the option to reenable in SettingsView.

Gurmukh Singh
  • 1,875
  • 3
  • 24
  • 62

2 Answers2

4

No you cannot. However, a good UI/UX design will be careful before burning the one-time chance of asking for permissions. Instead, use a user friendly UI to explain why you need certain permissions. For example, I often found it frustrating to implement a permission display view, and handle various async permission requests in a seperate view model. So I recently made a SwiftUI package:

PermissionsSwiftUI

             enter image description here     enter image description here

PermissionSwiftUI is a package to beautifully display and handle permissions.

EmptyView()
.JMPermissions(showModal: $showModal, for: [.locationAlways, .photo, .microphone])       

For a SINGLE line of code, you get a beautiful UI and the permission dialogs. It already supports 7 OUT OF 12 iOS system permissions. More features coming

Full example

struct ContentView: View {
    @State var showModal = false
       
    var body: some View {
        Button(action: {showModal=true},
               label: {Text("Ask user for permissions")})
           .JMPermissions(showModal: $showModal, for: [.locationAlways, .photo, .microphone])
       }
   }

To use PermissionsSwiftUI, simply add the JMPermission modifier to any view. Pass in a Binding to show the modal view, and add whatever permissions you want to show.

Jevon Mao
  • 176
  • 3
3

The short answer is no, you can't ask the user again if he once disabled the push-notifications for your app. What you can do, is navigating the user to the settings in their phone to allow push-notifications again.

The code snippet in SwiftUI for the button would be:

Button(action: {
    guard let url = URL(string: UIApplication.openSettingsURLString) else { return }
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}, label: {
    Text("Allow Push")
})

I would also refer to this question: How to ask notifications permissions if denied?

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45