Inherits from NSObject
Declared in SMPushClient.h
SMPushClient.m

Overview

An SMPushClient provides a high level interface to interacting with StackMob’s push service. A new client must be given an API Version and OAuth credentials in order to communicate with your StackMob application.

SMPushClient sets default values for other configuration settings which may be set as necessary by your application.

SMPushClient exposes a defaultClient for applications which use a globally available client to share configuration settings.

Include #import "StackMobPush.h" in your application files to gain access to SMPushClient and SMPushToken methods.

Setting Up A Basic Push Application

Upload your certs to StackMob

You can do this by reading through the Push API tutorial on StackMob.

Get a device token and initialize your push client

Set up an iOS app to register for remote notifications by calling registerForRemoteNotificationTypes when your app launches, most likely in application:didFinishLaunchingWithOptions:. Also initialize a push client.

// In application:didFinishLaunchingWithOptions:

// Assuming you declared the variable `SMPushClient *pushclient;`
// Public and private keys refer to your StackMob app's credentials.  Get them from the __Manage App Info__ link in the left column on the Dashboard
pushClient = [[SMPushClient alloc] initWithAPIVersion:@"0" publicKey:@"publicKey" privateKey:@"privateKey"];

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
    (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

Next, implement the method application:didRegisterForRemoteNotificationsWithDeviceToken: which will be called when your app successfully registers with APNS.

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [[token componentsSeparatedByString:@" "] componentsJoinedByString:@""];
    // Persist your user's accessToken here if you need

    // the user parameter is simply an arbitrary string to help you map a name to the device token
    [pushClient registerDeviceToken:token withUser:@"randomuser" onSuccess:createdSuccessBlock onFailure:createdFailureBlock];
}

Send push messages

You are now ready to broadcast messages! You can use any of the following methods to send push notifications:

  • broadcastMessage:onSuccess:onFailure:
  • sendMessage:toUsers:onSuccess:onFailure:
  • sendMessage:toTokens:onSuccess:onFailure:

Tasks

Initialize

Registering a Device Token

Sending Push Notifications

Retrieving Tokens For Users

Deleting a Device Token

Instance Methods

broadcastMessage:onSuccess:onFailure:

Broadcast a message to all registered devices for this app.

- (void)broadcastMessage:(NSDictionary *)message onSuccess:(SMSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock

Parameters

message

The message to send. See Apple’s docs for details on format.

successBlock

The block to call on success

failureBlock

The block to call on failure

Discussion

The message will be sent to all users on all platforms.

Declared In

SMPushClient.h

deleteToken:onSuccess:onFailure:

Deletes the token from StackMob’s server.

- (void)deleteToken:(id)token onSuccess:(SMSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock

Parameters

token

A token represented by either a device token string or an SMPushToken object.

successBlock

The block to call on success.

failureBlock

The block to call on failure.

Declared In

SMPushClient.h

getTokensForUsers:onSuccess:onFailure:

Get the tokens registered for the given users.

- (void)getTokensForUsers:(NSArray *)users onSuccess:(SMResultSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock

Parameters

users

An array of username strings to query.

successBlock

The block to call on success.

failureBlock

The block to call on failure.

Discussion

If successful the onSuccess block will be called with an NSDictionary mapping username strings to NSArrays of SMPushToken objects.

Declared In

SMPushClient.h

initWithAPIVersion:publicKey:privateKey:

Initialize with only the most basic parameters and defaults for the rest.

- (id)initWithAPIVersion:(NSString *)appAPIVersion publicKey:(NSString *)publicKey privateKey:(NSString *)privateKey

Parameters

appAPIVersion

The API version of your StackMob application which this client instance should use.

publicKey

Your StackMob application’s OAuth publicKey.

privateKey

Your StackMob application’s OAuth privateKey.

Declared In

SMPushClient.h

initWithAPIVersion:publicKey:privateKey:pushHost:

Initialize specifying all parameters.

- (id)initWithAPIVersion:(NSString *)appAPIVersion publicKey:(NSString *)publicKey privateKey:(NSString *)privateKey pushHost:(NSString *)host

Parameters

appAPIVersion

The API version of your StackMob application which this client instance should use.

publicKey

Your StackMob application’s OAuth publicKey.

privateKey

Your StackMob application’s OAuth privateKey.

host

The url of StackMob’s push server

Declared In

SMPushClient.h

registerDeviceToken:withUser:onSuccess:onFailure:

Register a device token for push notifications.

- (void)registerDeviceToken:(id)token withUser:(NSString *)username onSuccess:(SMSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock

Parameters

token

The device token for this device.

username

A name to be associated with the device token.

successBlock

The block to call on success.

failureBlock

The block to call on failure.

Discussion

Device tokens must be registered before they can be pushed to. Each token is associated with a username on the server, and this can be used to simplify pushing to users on multiple devices and platforms via sendMessage:toUsers:onSuccess:onFailure:. The username can correspond to a regular StackMob username, but it doesn’t have to. You can also ignore usernames altogether by specifying an arbitrary value and working entirely with tokens. This call will overwrite existing tokens.

Declared In

SMPushClient.h

registerDeviceToken:withUser:overwrite:onSuccess:onFailure:

Register a device token for push notifications.

- (void)registerDeviceToken:(id)token withUser:(NSString *)username overwrite:(BOOL)overwrite onSuccess:(SMSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock

Parameters

token

The device token for this device.

username

A name to be associated with the device token.

overwrite

When set registering an existing token will succeed.

successBlock

The block to call on success.

failureBlock

The block to call on failure.

Discussion

Device tokens must be registered before they can be pushed to. Each token is associated with a username on the server, and this can be used to simplify pushing to users on multiple devices and platforms via sendMessage:toUsers:onSuccess:onFailure:. The username can correspond to a regular StackMob username, but it doesn’t have to. You can also ignore usernames altogether by specifying an arbitrary value and working entirely with tokens. This call will fail if the token is already registered unless the overwrite flag is set.

Declared In

SMPushClient.h

sendMessage:toTokens:onSuccess:onFailure:

Send a message to specific tokens.

- (void)sendMessage:(NSDictionary *)message toTokens:(NSArray *)tokens onSuccess:(SMSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock

Parameters

message

The message to send. See Apple’s docs for details on format.

tokens

An array containing either device token strings or SMPushToken objects to push to.

successBlock

The block to call on success.

failureBlock

The block to call on failure.

Discussion

The tokens must have been registered with StackMob by registerDeviceToken:withUser:onSuccess:onFailure:

Declared In

SMPushClient.h

sendMessage:toUsers:onSuccess:onFailure:

Send a message to a particular set of users

- (void)sendMessage:(NSDictionary *)message toUsers:(NSArray *)users onSuccess:(SMSuccessBlock)successBlock onFailure:(SMFailureBlock)failureBlock

Parameters

message

The message to send. See Apple’s docs for details on format.

users

An array of username strings to push to.

successBlock

The block to call on success.

failureBlock

The block to call on failure.

Discussion

The users must have been previously registered by registerDeviceToken:withUser:onSuccess:onFailure:

Declared In

SMPushClient.h