2015-11-05 1 views
1

Dans mon application, j'essaie de recevoir une notification push, j'ai créé un certificat et l'a converti en fichier .pem pour tester dans le terminal, et copié le certificat sur le serveur, quand j'envoie la notification du serveur n'est pas reçu à la fin de l'appareil. Mon code comme suitne pas recevoir de notification push - 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]; 

} } 

code côté serveur

$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

Avez-vous effectué un débogage? La requête à Apple réussit-elle? Si non, quelle est l'erreur? Si c'est le cas, que se passe-t-il sur l'appareil? Regardez https://developer.apple.com/library/ios/technotes/tn2265/_index.html pour les étapes de débogage. – Avi

+0

Lorsque j'envoie une notification du serveur, il affiche "Deliver Successful" mais l'appareil ne reçoit aucune notification –

Répondre

2

Il peut y avoir problème de certificat en compte développeur, ou peut-être votre appareil n'est pas ajouté dans le profil de provisionnement. Vous pouvez le vérifier en recréant le profil d'approvisionnement.