Our last Tutorial will cover how to use push notifications to send messages to your users using Apples Push Notification service. As you know, you must register your application with Apple in order to perform push notifications... It is important to note that despite the fact that this tutorial focuses on an iPhone app, the API you have created can send notifications to Android, Blackberry and WinPho devices, without any alteration.
If you wish, you can download the complete Part V project file from Download Tutorial Part 5 Sample Files and then unzip and then either run newapp again from this directory to use this application or copy the files that are enclosed to the application directory you created in the previous section Installing FatFractal allowing the copy to replace all the previous files.
If you have not scaffolded your app already, it is not a problem, just run ffef newapp, using your subdomain, as before from the directory with the Part II files you have just downloaded and it will set up all the things that you need.
> $HOME/ff/ffnsbin/ffef newapp hoodyoodoo <your subdomain>
Note: The subdomain refers to the organization name you signed up with, e.g. acme in acme.fatfractal.com
Note: When you download the project, the client application is configured to use a API that is already deployed and populated with data. This means you can run the application on your simulator immediately. To use your API, you should edit the Hoodyoodoo.java file as before to point to the API that you have set up.
We will now modify our event handler that we set up in Tutorial Part 3 to send out a push notification whenever a new top rated celebrity is created.
There are three ways that we can do this:
While #1 is simpler to implement and more efficient for the API to process than #2 or #3, in this tutorial we will implement #3 as it will better serve to illustrate the general case solution.
First, we will create a new javascript file called TopCelebrityEventHandlers.js and add it to the ff-scripts directory for my application. As before, the basic structure of the file is:
var ff = require('ffef/FatFractal');
function handleTopCelebrityUpdate(json) {
… do some stuff
}
exports.handleTopCelebrityUpdate = handleTopCelebrityUpdate;
We want to create a function that will extract the first name and last name of the Celibrity from the event, create a message and then send to all users of the application.
We can do the first two in a single line:
var messageString = data.firstName + " " + data.lastName + " is our new Top Celebrity!"
To get your user information, you can use the very cool getAllGuids(url) method:
var userGuids = ff.getAllGuids("/FFUser");
Then, to send out the push notification, you can use the sendPushNotifications(guids, message) method:
var ff = require('ffef/FatFractal');
function handleTopCelebrityUpdate(json) {
data = JSON.parse(json);
var userGuids = ff.getAllGuids("/FFUser");
var messageString = data.firstName + " " + data.lastName + " is our new Top Celebrity!"
if (userGuids.length != 0) ff.sendPushNotifications (userGuids, messageString);
}
exports.handleTopCelebrityUpdate = handleTopCelebrityUpdate;
Then we modify the application.ffdl to add the new Event Handler to the configuration. we do this by adding the following line:
[code language="html"] CREATE HANDLER TopCelebUpdate ON /TopCelebrity Update as javascript:var h = require ('scripts/TopCelebrityEventHandlers'); h.handleTopCelebrityUpdate (FF_EVENT_DATA_JSON);To make your API aware of these new items, you need to deploy again (have you noticed how little you have to fiddle around with your API so far??) by doing the following:
From the hoodyoodoo application directory, deploy the modified hoodyoodoo API to the NoServer Public Cloud, you use the command line deployFFFabric:
> $HOME/ff/ffnsbin/ffef deployFFFabric
In a few seconds, your updated application will be available at http://<your subdomain>.fatfractal.com/hoodyoodoo
Assuming that the FatFractal Engine is running, you can also deploy to your development machine using the command line deploylocal:
> $HOME/ff/ffnsbin/ffef deploylocal
In a few seconds, your modified application will be available at http://localhost:8080/hoodyoodoo
You must also configure your application to handle push notifications as follows:
Edit the AppDelegate didFinishLaunchingWithOptions method needs to include this line of code:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
Next, the AppDelegate needs to implement the RemoteNotification delegate methods:
// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
NSLog(@"didRegisterForRemoteNotifications called");
[[FatFractal main] registerNotificationID:[devToken description]];
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"Error in registration. Error: %@", err);
}
And lastly, we add the following to handle a notification when it is received.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Received push notification %@", userInfo);
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Been outdone!"
message:[userInfo valueForKeyPath:@"aps.alert"]
delegate:nil
cancelButtonTitle:@"Okie Dokie"
otherButtonTitles:nil];
[prompt show];
}
That's it, the next time a TopCelebrity is created or updated, then a push notification will be sent to any application user that has authorized the application to receive them.
Now for the bad news - in order to actually see a push notification, you must go through the complex procedure of signing up for a developer program, registering your application with the AppStore, getting provisioning and ssl push certificates and can only test from a phone device (not the simulator). If you want to see push notifications in action, get ahold of us and we will be happy to add your add your phone to the provisioned devices so you can test it from our API.
I will be adding more on how to set up your own app for push - it is a pain, but a cool feature nonetheless.
Here is the result:
Have fun!