Remote Apple Push notifications for authenticated user

Discussion in 'iOS Programming' started by sscotti, Jan 28, 2015.

  1. sscotti macrumors newbie

    Joined:
    Oct 25, 2003
    Location:
    Minnesota
    #1
    I am a newbie when it comes to Xcode and IOS development, although now seems like a good time to learn with the introduction of Swift. I've learned some basics and I wanted to know if there is a way to to register users and setup Apple remote push notifications in an APP that uses WKWebView to display a responsive website in the in App browser. I have that setup, and I've set up the notification registration in AppDelegate.swift so that it works generically for all users, signed in or not. I also have everything setup in terms of certificates with a local testing environment using a PHP script to send notifications, and I also have the nice little OS X app PushMeBaby for testing purposes.

    I do have a few questions though. I might want to invoke the registration for notifications only after the user has signed in to the website displayed in the in App browser. The site is PHP/MySQL, and I need to set up some method by which to register then user and preferably return the device ID and the user ID to the remote server so that it can send out notifications to registered users.

    Also wondering, just in general terms, how the remote server collects device ID's. Is this done when the user signs up for notifications by sending a response to the server, or is there another method by which to get an array of all device ID's that are registered (i.e. from the Apple service).

    The basic code that I have in AppDelegate.swift is:

    Code:
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            
            // Customized code, below is for Notification registration
            
            var types: UIUserNotificationType = UIUserNotificationType.Badge |
                UIUserNotificationType.Alert |
                UIUserNotificationType.Sound
            
            var settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
            
            application.registerUserNotificationSettings( settings )
            application.registerForRemoteNotifications()
            
            return true
        }
    
        func application( application: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData! ) {
            
            var characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
            
            var deviceTokenString: String = ( deviceToken.description as NSString )
                .stringByTrimmingCharactersInSet( characterSet )
                .stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
            
            println( deviceTokenString )
            
        }
        
        func application( application: UIApplication!, didFailToRegisterForRemoteNotificationsWithError error: NSError! ) {
            
            println( error.localizedDescription )
        }
    
    The basic features are working in the sandbox / development environment.
     
  2. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #2
    Just keep an field like.. user_token in your users table on your page.
    When the user opens the app, just check it's credentials and update the record of the user when needed.

    Then you will have the tokens of your registered users.

    Just as example. I'm using an unique 'session' for each user, everytime he/she logs in or gets back to the app. And it's stored locally.

    Everytime the app opens it will check the session together with unique id and posts its token.
    For UDID I use [[[UIDevice currentDevice] identifierForVendor] UUIDString]
    This might change once a while, so I also store it locally.

    In my php I check the 'credentials' and store the current token for the user :)
     

Share This Page