QuickBlox Developers (API docs, code samples, SDK)

SimpleSample-messages users-ios

From QuickBlox Developers (API docs, code samples, SDK)
Jump to: navigation, search

Contents

Sources

Project homepage on GIT — https://github.com/QuickBlox/quickblox-ios-sdk/tree/master/sample-messages

Download ZIP - https://github.com/QuickBlox/quickblox-ios-sdk/archive/master.zip


Overview

This sample demonstrates how to work with Messages (Push Notifications) QuickBlox API.
It allows to send and receive push messages.

It shows how to:

  1. Subscribes user for receive push messages (from other Users and from Admin panel)
  2. Send push notification to particular User
  3. Work with Rich Pushes

SimpleSample-messages_v2_1.png   SimpleSample-messages_v2_3.png


Guide: Get Started with Messages API

Get QuickBlox account

http://admin.quickblox.com/register

Create application on Admin panel

http://admin.quickblox.com/apps/new

Also you can look through 5 min guide.

Connect QuickBlox to your application

To get the information on how to connect to the QuickBlox.framework, please, refer to the IOS-how-to-connect-Quickblox-framework page.

Add Push Notifications to your application

Create APNS certificates

Each iOS applications that uses Apple Push Notifications must have APNS certificates, that must be uploaded to Admin panel. To get the information on how to create APNS certificates and upload them to Admin panel, please, refer to the How to create APNS certificates page.

Note: You must use valid Provisioning Profiles for your Xcode project.

How QuickBlox Push Notifications work

In order to use QuickBlox Messages APIs you must Create session & Sign In to QuickBlox OR just create session with User.

There are 2 ways of how you can integrate Push Notifications into your application:

  1. Create single user, which will be collect all your users' devices. It's simple way, it doesn't need to create Login screen in your application. Then - just go to Admin panel, Message module, write message in Simple mode & Send - all your users will receive this message.
  2. If you want to separately send Push Notification to particular user or group of users - each user must be logged in QuickBlox as separate user.

Subscribe User to receive Push Notifications

  1. Let's create test User on Admin panel and subscribe it to receive Push Notifications. Go to Users module on Admin panel & press Add new user button:

    AddNewUser.png

    Fill at least login & password fields and press Add user button

    SaveNewUser.png

    Also you can create new User through Users APIs

  2. Create session with user's parameters & subscribe to receive Push Notifications
    Use login & password from previous step
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
     
    // Set your QuickBlox application credentials here
    // ...
     
    QBASessionCreationRequest *extendedAuthRequest =[QBASessionCreationRequest request];
    extendedAuthRequest.userLogin = @"garry";
    extendedAuthRequest.userPassword = @"garry345s";                    
     
    [QBAuth createSessionWithExtendedRequest:extendedAuthRequest delegate:self]; 
     
    #pragma mark -
    #pragma mark QBActionStatusDelegate
     
    // QuickBlox queries delegate
    - (void)completedWithResult:(Result *)result{
     
        if(result.success) {
            // Create session result
            if([result isKindOfClass:QBAAuthSessionCreationResult.class]){
                // You have successfully created the session
     
                // Subscribe Users to Push Notifications
                [QBMessages TRegisterSubscriptionWithDelegate:self];
     
            // Subscribe User to Push Notifications result 
            }else if([result isKindOfClass:QBMRegisterSubscriptionTaskResult.class]){       
                // Now you can receive Push Notifications!
            }
     
        }else{
            NSLog(@"errors=%@", result.errors);
        }
    }
    }

    After that you ready to receive Push Notifications!


Note: You have to use lines bellow when you upload your application to Apple Store or create AdHoc:

#ifndef DEBUG
    [QBSettings useProductionEnvironmentForPushNotifications:YES];
#endif

Unsubscribe user from receiving push notification

If you no longer wish to receive messages - you can unsubscribe from receiving push notification

[QBMessages TUnregisterSubscriptionWithDelegate:self];
 
#pragma mark -
#pragma mark QBActionStatusDelegate
 
- (void)completedWithResult:(Result *)result{
    // Unsubscribe from receiving push notification result
    if(result.success && [result isKindOfClass:QBMUnregisterSubscriptionTaskResult.class]){        
        // You have successfully unsubscribed from receiving push notification
 
    }else{
        NSLog(@"errors=%@", result.errors);
    }
}

Send Push Notifications

Send Push Notifications from Admin panel

Just go to Messages module on Admin panel, choose Environment, Channel, type message and press Send message button:

SendPushNotifications.png

Your message will be delivered to all your users. You will see

NotifySuccess.png

and

Push.jpeg


Also you can set more options through Advanced mode like:

  • Group of users (using tags), that will receive message

    PushNotificationsTags.png

  • Delivery settings

    PushNotificationsDeliverySettings.png

Send Push Notifications from Application

Send Push Notifications to particular users

There are two types of push that you can send:

  • Apple based push notification
  • Simple push notification


Send Apple based push notification (you can send this push only for iOS users):

NSString *mesage = @"Hello man!";
NSMutableDictionary *payload = [NSMutableDictionary dictionary];
NSMutableDictionary *aps = [NSMutableDictionary dictionary];
[aps setObject:@"default" forKey:QBMPushMessageSoundKey];
[aps setObject:mesage forKey:QBMPushMessageAlertKey];
[payload setObject:aps forKey:QBMPushMessageApsKey];
 
QBMPushMessage *message = [[QBMPushMessage alloc] initWithPayload:payload];
 
// Send push to users with ids 292,300,1295
[QBMessages TSendPush:message toUsers:@"292,300,1395" delegate:self];
 
[message release];
 
#pragma mark -
#pragma mark QBActionStatusDelegate
 
- (void)completedWithResult:(Result *)result{
    // Send Push Notifications result
    if(result.success && [result isKindOfClass:QBMSendPushTaskResult.class]){       
        // You have successfully send push notifications
 
    }else{
        NSLog(@"errors=%@", result.errors);
    }
}

Send Simple push notification:

// Send push to users with ids 292,300,1295
[QBMessages TSendPushWithText:@"Hello World" toUsers:@"292,300,1295" delegate:self];
 
#pragma mark -
#pragma mark QBActionStatusDelegate
 
- (void)completedWithResult:(Result *)result{
    // Send Push Notifications result
    if(result.success && [result isKindOfClass:QBMSendPushTaskResult.class]){       
        // You have successfully send push notifications
 
    }else{
        NSLog(@"errors=%@", result.errors);
    }
}


Send push to group of users (also allow 2 types)

NSString *mesage = @"Hello man!";
NSMutableDictionary *payload = [NSMutableDictionary dictionary];
NSMutableDictionary *aps = [NSMutableDictionary dictionary];
[aps setObject:@"default" forKey:QBMPushMessageSoundKey];
[aps setObject:mesage forKey:QBMPushMessageAlertKey];
[payload setObject:aps forKey:QBMPushMessageApsKey];
 
QBMPushMessage *message = [[QBMPushMessage alloc] initWithPayload:payload];
 
// Send push to groups 'man' and 'car'
[QBMessages TSendPush:message toUsersWithAnyOfTheseTags:@"man,car" delegate:self];
 
[message release];
 
#pragma mark -
#pragma mark QBActionStatusDelegate
 
- (void)completedWithResult:(Result *)result{
    // Send Push Notifications result
    if(result.success && [result isKindOfClass:QBMSendPushTaskResult.class]){       
        // You have successfully send push notifications
 
    }else{
        NSLog(@"errors=%@", result.errors);
    }
}

Add Rich Push Notifications to your application

Rich Push Notifications allow to delivery some content (images, video, HTML page) to you users.

Upload Rich Content

First, you must create rich content or upload you own through Admin panel. Let's upload picture.

Go to Content module page on Admin panel, сhoose any image on your hard disk and press Upload button

UploadContentToAdmin.png

Connect Rich Content to push

While creating Push body, press Add Content button

ConnectContentToPush.png

Next, check your uploaded image and press Add Selected button

AddSelected.png

Image will be connected to Push Notifications. Finally, choose Environment, Channel, write message and press Send message button.

SendRichPush.png

Congrats! Rich Push Notifications will be delivered to you users!

Retrieve Rich Content

As you know, push notification can't contain large amounts of data. So, all what we did - just sent push notification with link to rich content. Next, you must download this content and show it to your users. Don't worry about this - It's easy because you use QuickBlox!

As you know, when you open application from push or push retrieved on opened application - AppDelegate's application:didReceiveRemoteNotification: method will called. This method contains userInfo param that contains push body. So, let's extract link to rich content & download it:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
 
    // Get push message
    NSString *message = [[userInfo objectForKey:QBMPushMessageApsKey] objectForKey:QBMPushMessageAlertKey];
 
    // Get link to rich content
    NSString *richContent = [[userInfo objectForKey:QBMPushMessageApsKey] objectForKey:QBMPushMessageRichContentKey];
 
    // Download rich content
    [QBContent TDownloadFileWithBlobID:[richContent intValue] delegate:self];
}
 
#pragma mark -
#pragma mark QBActionStatusDelegate
 
// QuickBlox API queries delegate
-(void)completedWithResult:(Result*)result{
 
    // Download rich content result
    if(res.success && [result isKindOfClass:[QBCFileDownloadTaskResult class]]){
        QBCFileDownloadTaskResult *res = (QBCFileDownloadTaskResult *)result;
 
        // show image
        imageView.image = [UIImage imageWithData:res.file];
    }
}

You will see

SimpleSample-messages_v2_3.png

Conclusion

Now you can play around QuickBlox Messages API, integrate Push Notifications into your own applications and more & more with QuickBlox!

Comments

Feel free to comment on this page using the form below.

blog comments powered by Disqus
Go up