Featured

2014年4月29日星期二

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];


......

}


沒有留言:

發佈留言