push notify

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 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

apns.php
<?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);
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值