push notify
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
{
// Other Code
NSLog(@"Registering for push notifications...");
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
for (id key in userInfo) {
NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
}
}
Q:Failed to get token, error: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的权利字符串" UserInfo=0x1675e0 {NSLocalizedDescription=未找到应用程序的“aps-environment”的权利字符串}
A:
APP IDs
config -> Push SSL Certificate [Development , Production]
aps_developer_identity.cer
aps_production_identity.cer
Root Terminal
1,cd /System/Library/CoreServices
2,open -a "Directory Utility.app"
3.选取“编辑”>“启用 Root 用户”。
4.为 root 用户输入安全密码,然后在“验证”栏再次输入它,最后点按“好”。
apns_dev.pem
openssl pkcs12 -clcerts -nokeys -out apns_dev_cert.pem -in /Users/Adam/Desktop/apns_dev_cert.p12
openssl pkcs12 -nocerts -out apns_dev_key.pem -in /Users/Adam/Desktop/apns_dev_key.p12
openssl rsa -in apns_dev_key.pem -out apns_dev_key_noenc.pem
cat apns_dev_cert.pem apns_dev_key_noenc.pem > apns_dev.pem
cp apns_dev.pem /Users/Adam/Desktop/
<?php
$deviceToken = "deviceToken";
$body = array("aps" => array("alert" => 'message', "badge" => 1, "sound" => 'dtmf-s.aif'));
$ctx = stream_context_create();
stream_context_set_option($ctx, "ssl", "local_cert", "apns_dev.pem");
$fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstrn";
return;
}
print "Connection OK/n";
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "/n";
fwrite($fp, $msg);
fclose($fp);
?>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
{
// Other Code
NSLog(@"Registering for push notifications...");
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
for (id key in userInfo) {
NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
}
}
Q:Failed to get token, error: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的权利字符串" UserInfo=0x1675e0 {NSLocalizedDescription=未找到应用程序的“aps-environment”的权利字符串}
A:
APP IDs
config -> Push SSL Certificate [Development , Production]
aps_developer_identity.cer
aps_production_identity.cer
Root Terminal
1,cd /System/Library/CoreServices
2,open -a "Directory Utility.app"
3.选取“编辑”>“启用 Root 用户”。
4.为 root 用户输入安全密码,然后在“验证”栏再次输入它,最后点按“好”。
apns_dev.pem
openssl pkcs12 -clcerts -nokeys -out apns_dev_cert.pem -in /Users/Adam/Desktop/apns_dev_cert.p12
openssl pkcs12 -nocerts -out apns_dev_key.pem -in /Users/Adam/Desktop/apns_dev_key.p12
openssl rsa -in apns_dev_key.pem -out apns_dev_key_noenc.pem
cat apns_dev_cert.pem apns_dev_key_noenc.pem > apns_dev.pem
cp apns_dev.pem /Users/Adam/Desktop/
php server
cd /etc
cp php.ini.default php.ini
cd /etc/apache2
vi httpd.conf
LoadModule php5_module libexec/apache2/libphp5.so
: w
sudo apachectl start
sudo apachectl -v
apachectl restart
<?php
$deviceToken = "deviceToken";
$body = array("aps" => array("alert" => 'message', "badge" => 1, "sound" => 'dtmf-s.aif'));
$ctx = stream_context_create();
stream_context_set_option($ctx, "ssl", "local_cert", "apns_dev.pem");
$fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstrn";
return;
}
print "Connection OK/n";
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "/n";
fwrite($fp, $msg);
fclose($fp);
?>