← Back to Push Notifications for WordPress
Download the Library
Here is the library to register for push notifications on your website running Push Notifications for WordPress and receive them:
Adding to your project
Now that you've downloaded the library, you're ready to add it to your own project. Here's what you do:
- Open your project in Xcode.
- Navigate with Finder to where you uncompressed the library and drag the PushNotifications.framework folder into your project in Xcode.
- Make sure Copy items ... is selected.
- Press Finish button.
Build your application. If you encounter an error, double-check the steps above. If it runs without error, you're ready to integrate push notifications.
Integrate Push Notifications into Your App Delegate
To simplify the integration of Push Notifications for WordPress in your iOS app follow these steps:
- Import the
<PushNotifications/PushNotifications.h>
header. - Make the AppDelegate conform the
DSRestClientDelegate
protocol. - Add the
@property (nonatomic, strong) DSRestClient *restClient;
in the AppDelegate interface:@interface AppDelegate () <DSRestClientDelegate> @property (nonatomic, strong) DSRestClient *restClient; @end
- In the
application:didFinishLaunchingWithOptions:
method add these lines of code:if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:( UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:( UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; }
- Implement the
application:didRegisterForRemoteNotificationsWithDeviceToken:
andapplication:didFailToRegisterForRemoteNotificationsWithError:
methods:- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Important note: If the user restores backup data to a new device or computer, // or reinstalls the operating system, the device token changes. self.restClient = [[DSRestClient alloc] init]; self.restClient.delegate = self; [self.restClient registerWithUrl:@"http://[SERVER]/pnfw/register/" andToken:deviceToken]; } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { // Manage failure }
Where [SERVER] is the domain of the server where you have previously installed and configured Push Notifications for WordPress.
You can enable OAuth request signing using:self.restClient = [[DSRestClient alloc] initWithKey:@"[CONSUMER_KEY]" andSecret:@"[CONSUMER_SECRET]"];
- Implement the
DSRestClientDelegate
methods, including:restClientRegistered:
andrestClient:registerFailedWithError:
.
Adding the "-ObjC" Linker Flag
- Select the project file from the project navigator on the far left side of the window.
- Select the target for where you want to add the linker flag.
- Select the Build Settings tab
Choose All to show all Build Settings. - Scroll down to the Linking section, and double-click to the right of where it says Other Linker Flags.
- A box will appear, Click on the + button to add a new linker flag.
- Type
-ObjC
and press enter.
Disable Bitcode option
- Select the project file from the project navigator on the far left side of the window.
- Select the target for where you want to disable Bitcode option.
- Select the Build Settings tab
Choose All to show all Build Settings. - Scroll down to the Build Options section, click to the right of where it says Enable Bitcode and set it to No.
App Transport Security Notes (iOS 9 or later)
iOS 9.0 has an "App Transport Security" which encourages developers to use HTTPS instead of HTTP (more info here).
If your server is not protected by HTTPS by default you'll get the error: "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."
You should add an exception for specific domains in your Info.plist
:
<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>yourdomain.com</key> <dict> <key>NSExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSIncludesSubdomains</key> <true/> </dict> </dict> </dict>
Receiving Push Notifications
You can retrieve notification data with the following code in the AppDelegate:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // This method is called if the app is running in the foreground, but also // if the user press "show" on the notification AND the app is in the background NSDictionary *aps = userInfo[@"aps"]; NSString *title = aps[@"alert"]; NSString *eventId = userInfo[@"id"]; if (application.applicationState == UIApplicationStateActive) { // If the application is foremost and visible when the system delivers the notification, // no alert is shown, no icon is badged, and no sound is played. So we need to display a // notification to the user when the app is actually running in the foreground ... show an alert ... } else { // App was just brought from background to foreground because the user tapped on the notification ... do something with id and title ... } }
Subscribing
This method allows client device to subscribe itself on the server. A user is created with the specified email address and role "App Subscriber." A welcome email is automatically sent to that address with the login details. If an app subscriber was already registered with that email address it is no longer created, but it is associated with the new token.
restClient.delegate = self; [restClient linkWithUrl:@"http://[YOUR WORDPRESS SERVER]/pnfw/register/" andEmail:email];
Those delegate methods are called:
- (void)restClientLinked:(DSRestClient *)client withEmail:(NSString *)email andCustomParameters:(NSDictionary *)customParameters; - (void)restClient:(DSRestClient *)client linkFailedWithError:(NSError *)error;
You can add any custom fields to subscribers using the hook pnfw_register_custom_parameters and use this method:
restClient.delegate = self; [restClient linkWithUrl:@"http://[YOUR WORDPRESS SERVER]/pnfw/register/" andEmail:email andCustomParameters:@{@"key": value}];
Unregistering
This method allows client device to unregister itself from push notifications. If the last token associated with an anonymous user is removed, the user is also removed.
restClient.delegate = self; [restClient unregisterWithUrl:@"http://[YOUR WORDPRESS SERVER]/pnfw/unregister/"];
Those delegate methods are called:
- (void)restClientUnregistered:(DSRestClient *)client; - (void)restClient:(DSRestClient *)client unregisterFailedWithError:(NSError *)error;
Retrieving Posts
This method returns a list of posts filtered for the saved device token (i.e. user/device). For each post are returned only basic information.
NSDate *timestamp = nil; restClient.delegate = self; [restClient loadPostsWithUrl:@"http://[YOUR WORDPRESS SERVER]/pnfw/posts/" andTimestamp:timestamp];
Those delegate methods are called:
- (void)restClient:(DSRestClient *)client loadedPosts:(PNPosts *)posts; - (void)restClientPostsUnchanged:(DSRestClient *)client; - (void)restClient:(DSRestClient *)client loadPostsFailedWithError:(NSError *)error;
Retrieving specific Post
This method returns the details of the specified post.
restClient.delegate = self; [restClient loadPostWithUrl:@"http://[YOUR WORDPRESS SERVER]/pnfw/posts/" andIdentifier:identifier];
Those delegate methods are called:
- (void)restClient:(DSRestClient *)client loadedPost:(PNPost *)post; - (void)restClientPostUnchanged:(DSRestClient *)client; - (void)restClient:(DSRestClient *)client loadPostFailedWithError:(NSError *)error;
Retrieving Categories
This method allows client device to retrieve the list of post categories.
NSDate *timestamp = nil; restClient.delegate = self; [restClient loadCategoriesWithUrl:@"http://[YOUR WORDPRESS SERVER]/pnfw/categories/" andTimestamp:timestamp;
Those delegate methods are called:
- (void)restClient:(DSRestClient *)client loadedCategories:(PNCategories *)categories; - (void)restClientCategoriesUnchanged:(DSRestClient *)client; - (void)restClient:(DSRestClient *)client loadCategoriesFailedWithError:(NSError *)error;
Updating Categories
This method allows client device to set posts categories of which wants to receive push notifications.
restClient.delegate = self; [restClient updateCategoryWithUrl:@"http://[YOUR WORDPRESS SERVER]/pnfw/categories/" andIdentifier:identifier andExclude:exclude;
Those delegate methods are called:
- (void)restClientCategoryUpdated:(DSRestClient *)client; - (void)restClient:(DSRestClient *)client updateCategoryFailedWithError:(NSError *)error;
Retrieving User Categories
This method allows client device to retrieve the list of user categories.
NSDate *timestamp = nil; restClient.delegate = self; [restClient loadUserCategoriesWithUrl:@"http://[YOUR WORDPRESS SERVER]/pnfw/user-categories/" andTimestamp:timestamp];
Those delegate methods are called:
- (void)restClient:(DSRestClient *)client loadedUserCategories:(PNUserCategories *)categories; - (void)restClientUserCategoriesUnchanged:(DSRestClient *)client; - (void)restClient:(DSRestClient *)client loadUserCategoriesFailedWithError:(NSError *)error;
Updating User Categories
This method allows client device to set user categories.
restClient.delegate = self; [restClient updateUserCategoryWithUrl:@"http://[YOUR WORDPRESS SERVER]/pnfw/user-categories/" andIdentifier:identifier];
Those delegate methods are called:
- (void)restClientUserCategoryUpdated:(DSRestClient *)client; - (void)restClient:(DSRestClient *)client updateUserCategoryFailedWithError:(NSError *)error;