2011-09-20 32 views
10

को मैं इस के लिए ट्यूटोरियल की एक बहुत कुछ पढ़ा है और मैं सिर्फ ti पता चाहते थे कि अगर यह इसभेजने डिवाइस टोकन सर्वर

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

    NSString* newToken = [deviceToken description]; 

    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 


    NSString *urlString = [NSString stringWithFormat:@"http://myhost.com./filecreate.php?token=%@",newToken]; 

    NSURL *url = [[NSURL alloc] initWithString:urlString]; 

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 

    NSData *urlData; 
    NSURLResponse *response; 
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil]; 

} 

किसी भी सलाह अधिक तो स्वागत कर रहे हैं करने के लिए सही तरीका है।

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 

    const char* data = [deviceToken bytes]; 
    NSMutableString* token = [NSMutableString string]; 

    for (int i = 0; i < [deviceToken length]; i++) { 
     [token appendFormat:@"%02.2hhX", data[i]]; 
    } 


    NSString *urlString = [NSString stringWithFormat:@"http://myhost.com/filecreate.php?token=%@",token]; 

    NSURL *url = [[NSURL alloc] initWithString:urlString]; 

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
    NSData *urlData; 
    NSURLResponse *response; 
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil]; 

} 

मेरा आवेदन दोनों कोडों के साथ काम करता है, लेकिन सही तरीका क्या है?

+0

विधि जोड़ने? – Nekto

+1

ठीक दिखता है, लेकिन, मैं 1 होगा, त्रुटि पुनर्प्राप्ति तंत्र जोड़ें (यदि अनुरोध विफल रहता है तो आप अपना टोकन खो देंगे!) और बी, एसिंक्रोनस अनुरोध का उपयोग करें। – mja

+1

असीमित? आपका मतलब क्या है। कृपया मुझे त्रुटि पुनर्प्राप्ति तंत्र के लिए कुछ गाइड लाइन भी दें। कोड काम करता है लेकिन मैं सोच रहा था कि यह सही तरीका है, क्योंकि मैंने कहीं पढ़ा है कि 'एनएसएसटींग * नया टोकन = [डिवाइस टोकन विवरण];' स्ट्रिंग – Spire

उत्तर

18

रूप MJA टिप्पणी में कहा, यह त्रुटि वसूली तंत्र को जोड़ने के लिए और अतुल्यकालिक अनुरोध का उपयोग करें (यदि अनुरोध है कि आप अपने टोकन खो देंगे विफल रहता है) बेहतर है (पृष्ठभूमि में टोकन भेजने)

अपने AppDelegate.m में:

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
    NSString * token = [NSString stringWithFormat:@"%@", deviceToken]; 
    //Format token as you need: 
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    token = [token stringByReplacingOccurrencesOfString:@">" withString:@""]; 
    token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""]; 

    [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"apnsToken"]; //save token to resend it if request fails 
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"apnsTokenSentSuccessfully"]; // set flag for request status 
    [DataUpdater sendUserToken]; //send token 
} 

टोकन भेजने के लिए नया वर्ग बनाने के (या मौजूदा से एक का उपयोग):
DataUpdater.h

#import <Foundation/Foundation.h> 

@interface DataUpdater : NSObject  
+ (void)sendUserToken; 
@end 

DataUpdater.m

#import "DataUpdater.h" 

@implementation DataUpdater 

+ (void)sendUserToken { 
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"apnsTokenSentSuccessfully"]) { 
     NSLog(@"apnsTokenSentSuccessfully already"); 
     return; 
    } 

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myhost.com/filecreate.php?token=%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"apnsToken"]]]; //set here your URL 

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
    { 
     if (error == nil) { 
      [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"apnsTokenSentSuccessfully"]; 
      NSLog(@"Token is being sent successfully"); 
      //you can check server response here if you need 
     } 
    }]; 
} 

@end 

तो फिर तुम [DataUpdater sendUserToken] कॉल कर सकते हैं; अपने नियंत्रकों में जब इंटरनेट कनेक्शन दिखाई देता है, या समय-समय पर, या में - (शून्य) viewWillAppear तरीकों

मेरे सलाह देता है - (शून्य) या viewDidLoad:
1) मैं AFNetworking का उपयोग async अनुरोध और चेक भेजने के लिए सर्वर JSON उत्तर
2) कभी-कभी यह इस तरह Parse के रूप में सेवाओं का उपयोग करना बेहतर है पुश नोटिफिकेशन के साथ काम करने के लिए

+0

Thx दोस्त लेकिन यह बहुत पुराना सवाल है। मैं जवाब स्वीकार करता हूं, इससे किसी को फायदा हो सकता है। – Spire

+3

मुझे लगता है कि आपको [सिंक्रनाइज़] कॉल करना चाहिए (https://developer.apple।जानकारी संग्रहीत करने के बाद कॉम/लाइब्रेरी/मैक/प्रलेखन/कोको/संदर्भ/नींव/कक्षाएं/nsuserdefaults_class/संदर्भ/संदर्भ.html # // apple_ref/occ/instm/NSUser डिफ़ॉल्ट/सिंक्रनाइज़ करें)। – nsacerdote

0

didFinishLaunchingWithOptions विधि में

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

कोड की लाइनों के ऊपर करने के बाद, आपका कोड काम नहीं करता नीचे

#pragma mark - 
#pragma mark Push Notifications 
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
NSString *token_string = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""] 
          stringByReplacingOccurrencesOfString:@">" withString:@""] 
          stringByReplacingOccurrencesOfString: @" " withString: @""]; 
NSString* strURL = [NSString stringWithFormat:@"http://www.sample.com?device_token=%@&type=IOS",token_string]; 
strURL=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSLog(@"%@",strURL); 
NSData *fileData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]; 
NSLog(@"content---%@", fileData); 
} 
+0

कोड में किया गया थाफिशिश लॉन्चिंग विथोप्शन को पुश नोटिफिकेशन प्राप्त करने के लिए जोड़ना होगा –