我在IOS 8.1中遇到有关远程推送通知的问题。我使用以下代码用IOS 8将消息推送到iphone 6,但它没有用,设备什么也没收到。那个人可以帮我解决这个问题吗?非常感谢。
php代码:
<?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);
?>
目标c代码:
- (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;
}