Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register notification in iOS 9

I am using this to register notification :

if application.respondsToSelector("registerUserNotificationSettings:") {
                let types:UIUserNotificationType = (.Alert | .Badge | .Sound)
                let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)

                application.registerUserNotificationSettings(settings)
                application.registerForRemoteNotifications()

            } else {
                // Register for Push Notifications before iOS 8
                application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound)
            }

But it's not woking for iOS 9.

like image 600
Alok Avatar asked Oct 08 '15 06:10

Alok


People also ask

How to register for push notification on iOS?

You can click on Allow to register for notifications. Let us say you denied Notification permission and now want to choose for notifications, you can go to Settings -> Notifications - > Select the APP -> Toggle the notifications to ON. Toggle the Allow Notifications switch ON when you want to register for Push Notification on iOS.

How do I receive remote notifications from an iOS application?

Before an iOS application can receive remote notification it must register with APNS. APNS will generate a unique device token and return that to the iOS application. The iOS application will then take the device token and then register itself with the application server.

What is Apple Push Notification gateway service (APNs)?

At the center of push notifications in iOS is the Apple Push Notification Gateway Service (APNS). This is a service provided by Apple that is responsible for routing notifications from an application server to iOS devices. The following image illustrates the push notification topology for iOS:

How do I register for remote notifications with APNs?

Registration with APNS is handled in the FinishedLaunching method of the application delegate class by calling RegisterForRemoteNotificationTypes on the current UIApplication object. When an iOS application registers with APNS, it must also specify what types of remote notifications it would like to receive.


Video Answer


1 Answers

I have found solution :

 if application.respondsToSelector("registerUserNotificationSettings:") {
        if #available(iOS 8.0, *) {
            let types:UIUserNotificationType = ([.Alert, .Sound, .Badge])
            let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
        } else {
            application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
        }
    }
    else {
        // Register for Push Notifications before iOS 8
        application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
    }

The above code is working perfectly for me .

like image 75
Alok Avatar answered Dec 30 '22 16:12

Alok