2015-11-05 117 views
1

在我的应用我试图收到推送通知,我创建了一个证书,并将其转化为.pem文件中的终端测试,并复制证书到服务器,当我发送通知从服务器它没有在设备端收到。 我的如下没有收到推送通知 - IOS

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Override point for customization after application launch. 


if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
{ 
    // iOS 8 Notifications 
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 

    [application registerForRemoteNotifications]; 
} 
else 
{ 
    // iOS < 8 Notifications 
    [application registerForRemoteNotificationTypes: 
    (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)]; 
} 
} 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 

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

NSString *newString = [str stringByReplacingOccurrencesOfString:@" " withString:@""]; 
newString = [newString stringByReplacingOccurrencesOfString:@"<" withString:@""]; 
newString = [newString stringByReplacingOccurrencesOfString:@">" withString:@""]; 


    NSData *postData = [[NSString stringWithFormat:@"regId=%@", newString] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"<myurl>"]]]; 

    [request setHTTPMethod:@"POST"]; 

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

    [request setHTTPBody:postData]; 

    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

} } 

服务器端代码代码

$message = 'Hello'; 
$badge = 3; 
$sound = 'default'; 
$development = true; 

$payload = array(); 
$payload['aps'] = array('alert' => $message, 'badge' => intval($badge), 'sound' => $sound); 
$payload = json_encode($payload); 

$apns_url = NULL; 
$apns_cert = NULL; 
$apns_port = 2195; 

if($development) 
{ 
$apns_url = 'gateway.sandbox.push.apple.com'; 
$apns_cert = '/path/to/cert/cert-dev.pem'; 
} 
else 
{ 
$apns_url = 'gateway.push.apple.com'; 
$apns_cert = '/path/to/cert/cert-prod.pem'; 
} 

$stream_context = stream_context_create(); 
stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert); 

$apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context); 

// You will need to put your device tokens into the $device_tokens array yourself 
$device_tokens = array(); 

foreach($device_tokens as $device_token) 
{ 
$apns_message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload; 
fwrite($apns, $apns_message); 
} 

@socket_close($apns); 
@fclose($apns); 
+0

你有没有做过任何调试?对苹果公司的要求是否成功?如果不是,那么错误是什么?如果有,设备上发生了什么?查看https://developer.apple.com/library/ios/technotes/tn2265/_index.html中的调试步骤。 – Avi

+0

当我从服务器发送通知时,它显示“交付成功”但设备没有收到任何通知 –

回答

2

有可能是在开发者帐户的一些证书问题,或者可能是你的设备是不是在供应增加的个人资料。您可以通过重新创建配置文件来检查它。