Featured

顯示包含「aws」標籤的文章。顯示所有文章
顯示包含「aws」標籤的文章。顯示所有文章

2014年4月29日星期二

Setup Amazon SNS Mobile Push for ios App —6


21. Push Messages from one iphone to multiple end points


NSMutableArray *arr = [DataSource getFriendAWS:kk list:self.all];

NSString *msg= [NSString stringWithFormat:

        @"您在%@%@KTV邀請喔 ~ 快來看看吧 ~",begindate,self.subject.text];
   

  for( NSString *tt in arr) {
        
        if (tt.length>5) {

            dispatch_queue_t queue = dispatch_get_global_queue
               (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

            dispatch_async(queue, ^{
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
                });
                
               
                
                [[MessageBoard instance] pushToMobileEnd:msg to:tt];

                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                    NSLog(@"---Mobile Push Done--");
                });
            });

            
        }
    }

Setup Amazon SNS Mobile Push for ios App —5

18. Modify end point region in MessageBoard.m :


-(id)init
{
    self = [super init];
    if (self != nil) {
        snsClient = [[AmazonSNSClient alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
        snsClient.endpoint = [AmazonEndpoints snsEndpoint:US_WEST_1];//US_EAST_1

        sqsClient = [[AmazonSQSClient alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
        sqsClient.endpoint = [AmazonEndpoints sqsEndpoint:US_WEST_1];
        
        // Find the Topic for this App or create one.
        topicARN = [self findTopicArn];
        if (topicARN == nil) {
            topicARN = [self createTopic];
        }
        
        // Find the Queue for this App or create one.
        queueUrl = [self findQueueUrl] ;
        if (queueUrl == nil) {
            queueUrl = [self createQueue];
            
            // Allow time for the queue to be created.
            [NSThread sleepForTimeInterval:4.0];
            
            [self subscribeQueue];
        }
        
        // Find endpointARN for this device if there is one.
        endpointARN = [self findEndpointARN] ;
        
    }
    
    return self;
}


19. Modify End Point User Default Setting :


-(bool)createApplicationEndpoint{
    
    NSString *deviceToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"myDeviceToken"];
    if (!deviceToken) {
        [[Constants universalAlertsWithTitle:@"deviceToken not found!" andMessage:@"Device may fail to register with Apple's Notification Service, please check debug window for details"] show];
    }
    
    SNSCreatePlatformEndpointRequest *endpointReq = [[SNSCreatePlatformEndpointRequest alloc] init];
    endpointReq.platformApplicationArn = PLATFORM_APPLICATION_ARN;
    endpointReq.token = deviceToken;
    
    SNSCreatePlatformEndpointResponse *endpointResponse = [snsClient createPlatformEndpoint:endpointReq];
    if (endpointResponse.error != nil) {
        NSLog(@"Error: %@", endpointResponse.error);
        [[Constants universalAlertsWithTitle:@"CreateApplicationEndpoint Error" andMessage:endpointResponse.error.userInfo.description] show];
        return NO;
    }
    
    endpointARN = endpointResponse.endpointArn;
    [[NSUserDefaults standardUserDefaults] setObject:endpointResponse.endpointArn forKey:@"DEVICE_ENDPOINT"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    return YES;
}

20. Capture iPhone device push token and create AWS End Point 


 if ([[MessageBoard instance] createApplicationEndpoint]) 
                    NSLog(@" Add end point OK");










Setup Amazon SNS Mobile Push for ios App —4


17. In your project, add following to AppDelegate.m


      #import "MessageBoard.h"
      #import <AWSRuntime/AWSRuntime.h>
      #import "Constants.h"


     - (void)application:(UIApplication*)application 

   didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
    
    //Convert deviceToken to String Type

    const char* data = [deviceToken bytes];

    NSMutableString* tokenString = [NSMutableString string];
   
    for (int i = 0; i < [deviceToken length]; i++) {
        [tokenString appendFormat:@"%02.2hhX", data[i]];
    }
    NSLog(@"deviceToken String: %@", tokenString);
    
    [[NSUserDefaults standardUserDefaults] setObject:tokenString 

           forKey:@"myDeviceToken"];

    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{

        NSLog(@"Failed to register with error : %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:

    (NSDictionary *)userInfo {

    application.applicationIconBadgeNumber = 0;

    
    NSLog(@"remote notification: %@",[userInfo description]);

    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
    
    NSString *alert = [apsInfo objectForKey:@"alert"];

    NSLog(@"Received Push Alert: %@", alert);
    
  
    
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   ......


 application.applicationIconBadgeNumber = 0;

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:

     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound

     UIRemoteNotificationTypeAlert)];
    
    if(launchOptions!=nil){

        NSString *msg = [NSString stringWithFormat:@"%@", launchOptions];
        NSLog(@"%@",msg);
     
        NSDictionary *apsInfo = [launchOptions objectForKey:@"aps"];
        
        NSString *alert = [apsInfo objectForKey:@"alert"];
        NSLog(@"Received Push Alert launchoption: %@", alert);
         
        
        
        
    }
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [AmazonErrorHandler shouldNotThrowExceptions];


......

}


Setup Amazon SNS Mobile Push for ios App ---3



11. Download [aws-sdk-ios-samples]

12. Find this SNS Sample project

      SNS_SQS_MessageBoard

13.Open  Constants.h and modify the following 3 values

     constant.h

#define ACCESS_KEY_ID                   @"AKXX"

#define SECRET_KEY

                 @"SOXXX"

#define PLATFORM_APPLICATION_ARN        @"arn:aws:sns:us-east-1:XXX"



14. Switch to project view and select the Message Board target.
        In the Build Settings tab, make sure Code Signing Identity has been
         set to your developer id

15.Select the follwing frameworks and click Add:

*  AWSRuntime.framework
*  AWSSNS.framework
*  AWSSQS.framework

16. Play and Feel  !!!!

Setup Amazon SNS Mobile Push for ios App ---2


6. Log in to the AWS SNS Console and Click "Add a New App"

    to create a new application.

7. Choose the p12 file you exported from keychain

8. Click Load Credentials From File

9. Application ARN will be shown in Application Details Section,

    make note of it and you will need it in the following step.



Setup Amazon SNS Mobile Push for ios App ---1

1. Add new App IDs in (iOS Dev Center/Certificates, Identifier & Profiles/Certificates)
 
    and submit

 
   App ID Description: AWS Push
   App ID:  com.xxxx.awpush

2. click Edit to configure App ID.

     click the checkbox to enable Push Notifications

3.Create Certificate
   Create and uploaded your CSR file,
   Generate your certificate and  to download .cer file to your local machine.

4. Import cer file into your Keychain.

5. Save the exported p12

For more information, please read http://docs.urbanairship.com/build/ios.html