2012-07-07 4 views
3

Apple 알림 서비스를 사용하고 있습니다. 따라서이 기능에서 내 앱을 다운로드하는 각 장치 토큰을 등록해야하며, 알림을 보내려는 경우 다른 사람이 어떤 주소로 보내야하는지 알려야합니다.내 앱을 다운로드하는 각 장치 토큰을 등록해야합니까?

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

NSString *str = [NSString stringWithFormat:@"Error: %@",[error localizedDescription]]; 
NSLog(@"%@",str);} 

답변

3

Urban Airship과 같은 서비스를 사용하지 않으려는 경우. 데이터베이스 (아마도 MySQL)를 만들고 PHP와 같은 언어를 사용하여 장치 토큰을 POST 한 다음 데이터베이스에 넣을 수있는 파일을 만듭니다.

이 다음과 같은 코드를 사용합니다 아이폰 OS 측면에서

<?php 
$connection = mysql_connect("localhost","username","password"); 
if (!$connection){ 
    die('Error: ' . mysql_error()); 
} 

mysql_select_db("my_database", $connection); 

mysql_query("INSERT INTO tokens (token) 
VALUES ('$_POST[token]')"); 

mysql_close($connection); 
?> 

과 같을 것이다 PHP에서

:

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    NSString *urlString; 
    NSURL *url; 
    NSMutableURLRequest *request; 
    NSString *postString; 

    urlString = @"http://yoururl.com/apns/registerDevice"; 
    url = [NSURL URLWithString:urlString]; 
    request = [NSMutableURLRequest requestWithURL:url]; 
    postString = [NSString stringWithFormat:@"token=%@", [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"]; 
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 

    (void)[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    //Casting this to void makes the warning of unused expression go away. 
} 
+1

데이터베이스에 입력하기 전에 입력 내용을 살균하십시오. – hypercrypt

2

예, 당신은 당신이 간단하게하려는 경우, 난 강력하게 당신이 당신의 푸시 알림을 위해 도시 비행선을 사용하는 것이 좋습니다, 모든 장치 토큰을 얻어야한다.

+0

무료입니까? 및 각 장치 토큰을 저장하는 방법은 무엇입니까? –

+1

무료 기본 계획이 있습니다. 그들은 자신의 데이터베이스에 장치 토큰을 보관합니다. 마찬가지로 간단하게 유지하려면이 방법이 좋다고 말한 것 외에 다른 방법으로 자신의 데이터베이스를 구현하고 푸시 알림을 보낼 수 있습니다. 너 자신. – Lefteris

0

그것은 조금 늦게하지만 조심 아마를 NEVER

를 넣어
$_POST['ivar'] 

을 SQL 쿼리 문자열에 넣으면 더 좋은 방법입니다. SQL 인젝션으로 연락하십시오! 준비 쿼리를 사용하십시오

관련 문제