2014-02-25 31 views
0

我試圖將設備令牌傳遞給我的服務器,但面臨一些問題,因爲一切工作在我的數據庫中它存儲爲空我不在這裏我做錯了。設備令牌傳遞給服務器空

-(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://localhost/pushnofiy/insert.php?token=%@",token]; 

     NSURL *url = [[NSURL alloc] initWithString:urlString]; 
     NSLog(@"token %@",urlString); 
     NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
     NSLog(@"request %@ ",urlRequest); 

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

如果我打印urlString其顯示設備令牌,但我不知道爲什麼它不是在數據庫中插入。

代碼

<?php 

    mysql_connect(localhost,$username); 
    @mysql_select_db($database) or die("Unable to find Database");*/ 
    include "db.php"; 

    $token =$_GET["token"]; 

    $query = "INSERT INTO notifi (token) VALUES('$token')"; 

    mysql_query($query) or die (mysql_error("error")); 
    mysql_close(); 
?> 

我不知道是怎麼回事錯誤的,我請人說不出哪裏我做錯了。

謝謝。

+0

*旁註:*停止使用已棄用的mysql_ *函數。改爲使用[MySQLi](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual/en/book.pdo.php)。這是一個很好的[教程](http://j.mp/PoWehJ)PDO。此外,你的代碼受到SQL注入攻擊,你允許直接在查詢中插入GET值。 – Raptor

+0

存儲空字符串或未插入記錄?您的PHP代碼有語法錯誤,即無法運行。 – Raptor

+0

@ShivanRaptor請告訴我,在我們能幫忙之前,我會在什麼地方做錯的,它確實會幫助我們完整 – user3349668

回答

0

,最好添加錯誤恢復機制(如果請求失敗,你會失去你的令牌),並使用異步請求(在後臺發送令牌)

在你AppDelegate.m:

​​

要發送的令牌創建新類(或者利用現有的一個): 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]在你的控制器時,互聯網連接出現,或定期或在- (無效)viewDidLoad中或 - (無效)viewWillAppear中方法

我的建議: 1)我用AFNetworking發送異步請求,並檢查服務器JSON響應 2)有時候最好使用Parse等服務來處理推送通知

+0

我已經嘗試過我們的代碼也不工作 – user3349668

+0

@ user3349668 U上面的代碼? – Raju

+0

只有相同的代碼我已經使用buts不爲我工作 – user3349668

相關問題