0

Here are the details of the error when app throw error to register the notification:

<EXPR>:3:1: error: expected member name or constructor call after type name
Error
^

<EXPR>:3:1: note: use '.self' to reference the type object
Error
^
 .self

Here is the code which is running successfully in iOS 10.1

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
{
    print("Failed to register Notification Error  = ", Error)        
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
    print("Device Token = ", Data) 
}



func registerForRemoteNotification()
{
    if #available(iOS 10.0, *)
    {

        let center  = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.sound, .alert, .badge])
        { (granted, error) in
            if error == nil
            {
                UIApplication.shared.registerForRemoteNotifications()


            }
        }
    }
    else
    {
        if #available(iOS 8.0, *)
        {
            UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
            UIApplication.shared.registerForRemoteNotifications()

        }
        else
        {
           UIApplication.shared.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
        }
    }
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{

    if let dicAps = notification.request.content.userInfo["aps"] as? NSDictionary
    {
        AppUtilities.showAlertWithMessage(title: APP_TITLE as NSString, message: "\(dicAps.object(forKey: "msg")!)" as NSString)
    }
    completionHandler([.alert, .badge, .sound])
}

//Called to let your app know which action was selected by the user for a given notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
    print("User Info = ",response.notification.request.content.userInfo)
}

I am trying to implement an app which will run on iOS 8 to 10.1., it works fine in iPhone 5s with iOS 10.1.1. but when I run the app in the simulator with iOS 8.1, it always throws an error which is described above, please correct me if I missed anything.

Thank you.

Umang Loriya
  • 840
  • 8
  • 15
JK Patel
  • 858
  • 8
  • 22

1 Answers1

0

Sorry to say, but you'll need to find some hardware to test this functionality.

Push notifications are not available in the simulator. They require a provisioning profile from iTunes Connect, and thus are required to be installed on a device. That also means you'll probably have to be accepted into the apple iPhone developer program and pay your $99.

Source : How can I test Apple Push Notification Service without an iPhone?

Museer Ahamad Ansari
  • 5,414
  • 3
  • 39
  • 45