0

I need create in my app a code that receive a remote information and push it to user when app is on background, I read on web that I need to use didReceiveRemoteNotification on appDelegate, to use remote push notication. I read something about and I need keys and certificates, I do not understand how to use didReceiveRemoteNotification

I need to learn about to push remote notification and how to use. I would like a tutorial or example how create it using swift 2.3.

  • Possible duplicate of [How to setup push notifications in Swift](http://stackoverflow.com/questions/24899257/how-to-setup-push-notifications-in-swift) – Anand Nimje Jul 15 '16 at 04:25

1 Answers1

0

I used this link and I found it the most helpful

http://www.appcoda.com/push-notification-ios/

I used this app for testing

https://itunes.apple.com/us/app/easy-apns-provider-push-notification/id989622350?mt=12

This is the code I have in my AppDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    registerForPushNotifications(application)

    print(UIDevice.currentDevice().identifierForVendor!.UUIDString)

    return true
}


func registerForPushNotifications(application: UIApplication) {
    let notificationSettings = UIUserNotificationSettings(
        forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    print("Device Token:", tokenString)
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Failed to register:", error)
}
Asdrubal
  • 2,421
  • 4
  • 29
  • 37
  • Hello Asdrubal, thanks by help, but I did not understand, how the app receive the json, in the link that you post, the app received the json {"aps":{"alert":"Hello from AppCoda!","badge":1, "sound": "default"}}, but who send this json? Can I create a web service using PHP for example to send json data? But where I call this link? How the didReceiveRemoteNotification get this data to work? How to scheduled? – Alexandre C. do Carmo Jul 15 '16 at 01:54
  • Personally I use [Amazon SNS](http://docs.aws.amazon.com/sns/latest/dg/mobile-push-apns.html), but I have also seen people use [Parse](https://parse.com/tutorials/push-notifications). You can create a web server for APNs. This link might help you http://samvermette.com/145. I not sure exactly how everything happens between apple servers and your device – Asdrubal Jul 15 '16 at 02:48