2016-04-29 4 views
0

Я использую push-уведомление в своем приложении iOS.Push сообщение уведомления не получено в iOS

Я не могу получить сообщение с уведомлением push с сервера. Но я могу получить сообщение при использовании стороннего сервера APN Tester бесплатно.

В моем приложении я использую backend как .net webservice (i.e asmx file). Ниже приведен код, который я использую для push-уведомления.

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


[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
[[UIApplication sharedApplication] registerForRemoteNotifications]; 

NSLog(@"didFinishLaunchingWithOptions"); 

return YES; 
} 
/-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
NSLog(@"My token is: %@", deviceToken); 

NSString *deviceTokenString = [NSString stringWithFormat:@"%@", deviceToken]; 

NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken"); 

NSLog(@"My token is: %@", deviceTokenString); 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:deviceTokenString 
               message:@"devicetoken" 
               delegate:self 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
[alert show]; 

/
}*/ 
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
NSLog(@"My token is: %@", deviceToken); 
_token_data =deviceToken; 


NSString *title = [NSString stringWithFormat:@"%@",_token_data]; 
NSString *trimmedString = [title stringByReplacingOccurrencesOfString: @" " withString:@""]; 
trimmedString = [trimmedString stringByReplacingOccurrencesOfString: @"<" withString:@""]; 
trimmedString = [trimmedString stringByReplacingOccurrencesOfString: @">" withString:@""]; 

_tokenkey = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding]; 

NSString *soapFormat = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
         "<soap:Envelope xmlns:xsi=\"http:/ 
         "<soap:Body>\n" 
         "<AddAppleDeviceToken xmlns=\"http:/ 
         "<PortalId>0</PortalId>\n" 
         "<ModuleId>12</ModuleId>\n" 
         "<strDeviceToken>sdfssdfs</strDeviceToken>\n" 
         "</AddAppleDeviceToken>\n" 
         "</soap:Body>\n" 
         "</soap:Envelope>\n"]; 

NSURL *locationOfWebService = [NSURL URLWithString:@"http://www.mywebsite.com/push.asmx" 

wsDeviceToken.asmx"]; 

NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService]; 
NSString *msgLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapFormat length]]; 
[theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; 
[theRequest addValue:@"http:/ 
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPMethod:@"POST"]; 
/
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]]; 
NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; 
NSLog(@"soapFormat %@",soapFormat); 
if (connect) { 
    _webData = [[NSMutableData alloc]init]; 
    NSLog(@"Connection Establish"); 
} 
else { 
    NSLog(@"No Connection established"); 
} 

NSString * test = [NSString stringWithFormat:@"Your Device token is %@", trimmedString]; 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:test 
               message:@"devicetoken" 
               delegate:self 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
[alert show]; 
} 
- (void)application:(UIApplication *)application  didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { 
if (notificationSettings.types != UIUserNotificationTypeNone) { 
    NSLog(@"didRegisterUser"); 
    [application registerForRemoteNotifications]; 

    UIAlertView *alert33 = [[UIAlertView alloc] initWithTitle:@"didRegisterUser" 
                 message:@"didRegisterUser" 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert33 show]; 
} 
} 
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error 
{ 
NSLog(@"Failed to get token, error: %@", error); 
} 
- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
/
/
NSString *message = [[userInfo objectForKey:@"aps"] 
        objectForKey:@"alert"]; 
UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"" 
         message:message 
         delegate:nil 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:nil]; 
[alert show]; 
/
} 
#pragma - NSURLConnection delegate method 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
[_webData setLength: 0]; 
NSHTTPURLResponse * httpResponse; 
httpResponse = (NSHTTPURLResponse *) response; 
NSLog(@"HTTP error %zd", (ssize_t) httpResponse.statusCode); 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
[_webData appendData:data]; 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
NSLog(@"ERROR with theConenction"); 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ NSLog(@"DONE. Received Bytes: %lu", (unsigned long)[_webData length]); 
self.xmlParser = [[NSXMLParser alloc]initWithData:_webData]; 
[self.xmlParser setDelegate: self]; 
[self.xmlParser parse]; 
} 
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict { 
} 
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 
if ([elementName isEqualToString:@"AddAppleDeviceTokenResult"]) { 
    NSString *tmpstr = [[NSString alloc] init]; 
    tmpstr = [tmpstr stringByAppendingString:soapResults]; 
    [soapResults setString:@""]; 
    NSLog(@"AddAppleDeviceTokenResult %@",tmpstr); 

    if([tmpstr isEqualToString:@"DeviceToken Added in Database"]) 
    { 

     NSString * test = [NSString stringWithFormat:@"Your Device token is added in database"]; 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:test 
                 message:_tokenkey 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
     NSLog(@"Token added successfully"); 

     [[NSUserDefaults standardUserDefaults]setValue:@"1" forKey:@"isDeviceTokenAdded"]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 
    } 
} 
} 
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string { 
if (!soapResults) { 
    /
    soapResults = [[NSMutableString alloc] init]; 
    NSLog(@"soapResults %@",soapResults); 
} 
if([string length] > 0){ 
    NSString *cleanString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    if([cleanString length] >0){ 
     [soapResults appendString: string]; 
    } 
} 
} 

Любые пожелания Пожалуйста.

Заранее спасибо.

+0

Я не вижу, где вы добавили методы делегата didReceiveRemoteNotification и didReceiveRemoteNotification: completionHandler? – SeanLintern88

+0

Используете ли вы сервер Apple для производства или разработки? – Gruntcakes

+0

спасибо, мы используем разработки нажимной сервера ... – jejai

ответ

0

jejai Заменить этот метод

- (void)application:(UIApplication *)application 
    didReceiveRemoteNotification:(NSDictionary *)userInfo 
    { 
     NSString *message = [[userInfo objectForKey:@"aps"] 
         objectForKey:@"alert"]; 
     UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"" 
          message:message 
          delegate:nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles:nil]; 
     [alert show]; 
    } 

К

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler 
{ 

    NSString *message = [[userInfo objectForKey:@"aps"] 
        objectForKey:@"alert"]; 
    UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"" 
         message:message 
         delegate:nil 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:nil]; 
    [alert show]; 

}