Saturday, June 13, 2015

Remote / Push Notification

This is a tutorial to remote / push notifications.

1. Create a single view application project in Xcode. Select 'Swift' as the language.

2. Disable 'Use Auto Layout' in the storyboard file inspector.


3. Register notification types (user notification settings) and remote notifications by modifying function application in AppDelegate.swift as below:
    
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        let myNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound

        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: myNotificationType, categories: nil))
        
        application.registerForRemoteNotifications()
        
        return true
    }

4. Add the following codes to AppDelegate.swift:


    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        println("didRegisterForRemoteNotificationsWithDeviceToken: \(deviceToken)")
    }
    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        println("didFailToRegisterForRemoteNotificationsWithError: \(error)")

    }


5. Select Finder/Applications/Utilities/Keychain Access


Select Keychain Access/Certificate Assistant/Request a Certificate From a Certificate Authority...


Enter the Certificate Information and save the certificate to disk.


6. Go to the Apple Developer website. Choose the Member Center and sign in. Choose Certificates, Identifiers & Profiles.



Choose iOS Apps\Identifiers.


Click the '+' sign at the top right and enter the App ID Description:


Enter the Explicit App ID.


In App Services section, enable 'Push Notifications'.

Confirm the App ID and press 'Submit'.

Select the App ID just created and select 'Edit'. 

The Push Notifications item is shown as configurable:
Select 'Create Certificate...' under Development SSL Certificate.


Select 'Choose File...' and choose the .certSigningRequest file just downloaded.
Select 'Generate'.

Select Download.

Double click the file just downloaded.

7. Select 'Provisioning Profile' and 'All'.

Select '+' and 'iOS App Development'.

Press 'Continue' and enter the profile name in the Generate state.

Download the provisioning profile.

7. In Keychain Access, select 'My Certificates', then right click on the certificate just created. Select Export. Enter the passwords if required.  


This saves the exported file to the Personal Information Exchange (p12) format.


8. Open Xcode. Run the app with an iPhone/iPad, not the iOS simulator. It should print out:

didRegisterForRemoteNotificationsWithDeviceToken: <0b8c765e 19056ab3 ....

Keep this device token since it will be used later in the push Java file.

9. Enable the terminal by finding it at Applications/Utilities/Terminal.

Type nano in the command line. Save the file as *.java. You may use nano to edit the file. I prefer to use XCode.

The Java file is edited as below:


import java.io.*;
import com.notnoop.apns.*;

public class MyPush
{
public static void main(String[] args)
{
        String myCertPath = "RemoteNotificationP12.p12";
        String myPassword = "mypassword";
        String myToken = "0b8c765e19056ab34f4f609dd8ec37...";
        
        ApnsService service =
APNS.newService()
.withCert(myCertPath,myPassword)
.withSandboxDestination()
.build();
String myPayload = APNS.newPayload()
.alertBody(args[0])
            .badge(1)
            .sound("default")
.build();
service.push(myToken, myPayload);
}

}

Note that the space characters within the device token have to be removed.


10. Download apns-0.1.5-jar-with-dependencies.jar. Put it in together with *.java and *.p12 files in the same folder.

11. In the terminal, go to the directory of the three files. Compile the Java file with the javac command:

javac -cp apns-0.1.5-jar-with-dependencies.jar MyPush.java

Now the *.class file is created.



Then run the *.java file with the java command in the terminal:

java -cp apns-0.1.5-jar-with-dependencies.jar: MyPush "Hot News 123"

The iPad / iPhone should receive the push notification. There should be an alarm sound and an updated badge number.



No comments:

Post a Comment