0

i have a problem about remote push notification in IOS 8.1. I used the following code to push a message to iphone 6 with IOS 8, but it did not work, the device received nothing. Can sameone help me to solve this problem? Thanks a lot.

php code:

<?php
$passphrase = 'masterkey';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'cer/pushcert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 1, STREAM_CLIENT_CONNECT, $ctx);

if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
    $deviceToken=$deviceToken_in_database; //read device token from database
    $message = "here is test";

    $body['aps'] =array(
        'alert' => $message,
        'sound' => 'default'
    );

    $payload = json_encode($body);
    $msg= chr(0) . chr(0) . chr(32) . pack('H*', $deviceToken) . chr(0) . chr(mb_strlen($payload)) . $payload;
    $result_tmp = fwrite($fp, $msg);
    if (!$result_tmp){
        echo "failed";

    }else{
        echo "successful";
    }

fclose($fp);

?>

objective c code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // register remote Notification in ios 8
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
     UIUserNotificationTypeBadge |
     UIUserNotificationTypeSound
                                      categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    return YES;
}

……

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    // get push device token

    NSString * token = [NSString stringWithFormat:@"%@", deviceToken];
    //Format token as you need:
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
    token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
    //NSLog(@"deviceToken: %@", token);
    device_token=token;


}
AmaoQiu
  • 13
  • 4
  • Possibly duplicate Check this link , http://stackoverflow.com/questions/25909568/ios-8-enabled-device-not-receiving-push-notifications-after-code-update – V.V Oct 30 '14 at 10:59
  • already solved. reference: http://stackoverflow.com/questions/26602660/push-notifications-are-not-working-in-updated-version-from-ios7-to-ios8-1/26622457#26622457 – AmaoQiu Oct 31 '14 at 08:56

0 Answers0