This document is intended for the average Titanium Developer to enable push notifications. It is assumed that the reader already has a basic knowledge of Titanium and the APIs.
Today we will talk about Part 2 of 3 of Push Notifications, which begins with Google App Registration.
Register your application with Google
Create a Google API Project
Register with Appcelerator Cloud Services
Step 1 : Create a Google API Project
You will need to open the Google Developers Console by visiting https://cloud.google.com/console. From there you will need to sign in with your Google ID that you want associated with this project. Once signed in you will see a basic screen and a big red button that says ‘CREATE PROJECT’.
Enter a Project Name, and ignore Project ID. Project ID is randomly generated by Google to avoid duplication of IDs. Click the ‘Create’ button and the Google Developer.
Console should refresh. Now you should be on the projects Overview page. At the top of this page you will see Project ID and Project Number.
Copy the Project Number, since this will be used at a later point as the GCM Sender ID. On the left corner of the page there is an APIs & Auth section. Go to that page and a list of API items will populate. Turn On the Google Cloud Messaging for Android.
Again under APIs & Auth you will see ‘Credentials’, click there. Now you will see a section called ‘Public API Access’. Click the ‘CREATE NEW KEY’ button to generate an API Access Key.
A popup will appear, on that select ‘Server Key’. Another popup will appear asking you to enter IP Addresses in the text field, just click ‘Create’.
DO NOT ENTER ANY IP ADDRESSES
Now under the Public API access section there is a section for API key with a rather strange combination of letters & numbers. Copy that text and hang onto it. Now go to the Appcelerator Dashboard https://dashboard.appcelerator.com/.
As before select the app you are working with and click the ‘Cloud’ tab. On the left menu click ‘Settings & Configurations’. Then on the tabbed menu click ‘Android Push’. It will ask you for the GCM API Key and GCM Sender ID, which you should have saved. Enter those values in and click ‘Save Changes’.
Implement the code into a Common JS library
Ensure that you are using a Common JS library for notifications. Create a ‘notifications.js’ file inside the ‘lib’ folder. If the folder does not exists then create it.
iOS Code Setup
With iOS you must use Ti.Network.registerForPushNotifications the first time the user enters the application. After registering with the server, the function should return a device token. Save this device token in app properties.
Example : Ti.App.Properties.setString(‘deviceToken’, e.deviceToken);
However make sure you set the application properties on the callback and not inside the common JS library. These lib files can sometimes act strangely when it comes to saving app properties.
The device token that is generated will allow the application to receive notifications. You will be able to register the device with ‘Channels’. The server can push information to certain Channels. Say you want sports news updates. Then a Channel called ‘sports_updates’ could be created. Channels are not done on the server side. Creation is done on the user side. There is not a way to manually add a channel on the ACS Dashboard. Once a user subscribes to a channel for the first time then it is created.
Subscribing a device token to a channel is a function in the common JS library you created for Push Notifications. In this function we pass two variables. One being the channel and the other being the device token that was generated earlier. Each one is needed for the subscription.
You will also need to allow the user to unsubscribe from Channel based Pushed Notifications, which can be done using the unsubscribe function.
Now we need to create a sample page to test this all out with. It will have a view, label, and button. Clicking this button will subscribe the user to sports updates from the ACS server.
This will create a basic view with a label and button on top of the window. It will say ‘Sports’ and upon click will call the register function from the notifications lib file.
You can also have users set up on the ACS server and log them in and subscribe to channels. However the user account must exist. Generally one user is created for the public, such as the ‘push_notifications’ user we see in the code. This is perfect for just a basic app with notifications. If you want user sessions and give the ability for people to create their own accounts you will have to look into Titanium.Cloud.Users on the documentation.
Try it and leave a comment below to let us know if you had any problems following these steps.
We will continue next week with part 3.