我正在尝试将设备令牌传递给我的服务器,但面临一些问题,即我的数据库中的所有内容都可以存储为空,我不会在这里做错。
-(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 :(得分:0)
最好添加错误恢复机制(如果请求失败,您将丢失令牌)并使用异步请求(在后台发送令牌)
在你的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];在出现互联网连接的控制器中,或定期或在 - (void)viewDidLoad或 - (void)viewWillAppear 方法
我的建议: 1)我使用AFNetworking发送异步请求并检查服务器JSON响应 2)有时最好使用Parse等服务来处理推送通知