打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS 开发 实现iOS推送

实现iOS推送,已经总结的都特别的棒了。

http://blog.csdn.net/showhilllee/article/details/8631734

http://www.jianshu.com/p/fda61af94d09

高度概括一下:

APPID 需要有推送属性
 
 

1 开发推送证书的cer文件和p12文件
2 发布推送证书的cer文件和p12文件
3 开发和发布需要对应的pem文件


生成方法:

把.cer的SSL证书转换为.pem文件,执行命令:
openssl x509 -in
aps_development.cer -inform der -out PushChatCert.pem

把私钥Push.p12文件转化为.pem文件:

openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12


对生成的这两个pem文件再生成一个pem文件,来把证书和私钥整合到一个文件里:

cat PushChatCert.pem PushChatKey.pem > ck.pem


测试方法:


telnet gateway.sandbox.push.apple.com 2195

它将尝试发送一个规则的,不加密的连接到APNS服务。如果你看到上面的反馈,那说明你的MAC能够到达APNS。按下Ctrl+C关闭连接。
使用我们生成的SSL证书和私钥来设置一个安全的链接去链接苹果服务器:

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem



当你在最后的时候你看到这样说明你已经成功了:

CONNECTED(00000003)

*******balabalabalabala(省略)******


发布证书也是如此。
只是测试服务器变成:gateway.push.apple.com

code
 
 
=============
添加代码
-----------------//
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    [application registerForRemoteNotifications];
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"the token is %@", deviceToken.description);
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"the error is %@",error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
   
}

===================

真实环境与服务器联调,需要将需要的p12文件传给JAVA服务器。pem文件传给PHP服务器。
===================

java 服务器实现
http://blog.csdn.net/kerryzb/article/details/51507054

package com.kerryzb.util;  
  
import java.util.ArrayList;  
import java.util.List;  
  
  
  
  
import org.apache.commons.lang.StringUtils;  
  
import javapns.Push;  
import javapns.devices.Device;  
import javapns.devices.implementations.basic.BasicDevice;  
import javapns.notification.AppleNotificationServerBasicImpl;  
import javapns.notification.PushNotificationManager;  
import javapns.notification.PushNotificationPayload;  
import javapns.notification.PushedNotification;  
  
  
  
public class Test  
{  
    public static void main(String[] args) throws Exception  
    {  
        String deviceToken = "d4b3c5f3d497554f56f6f9791872666ae06e3b4e7abad6f4792dcd030007db91";  
        String alert = "给你发信息了";//push的内容  
        int badge = 3;//图标小红圈的数值  
        String sound = "default";//铃音  
  
        List<String> tokens = new ArrayList<String>();  
        tokens.add(deviceToken);  
        String certificatePath = "D:/ZshPush.p12";  
        String certificatePassword = "123456";//此处注意导出的证书密码不能为空因为空密码会报错  
        boolean sendCount = true;  
  
        try  
        {  
            PushNotificationPayload payLoad = new PushNotificationPayload();  
            payLoad.addAlert(alert); // 消息内容  
            payLoad.addBadge(badge); // iphone应用图标上小红圈上的数值  
             
            if (!StringUtils.isBlank(sound))  
            {  
                payLoad.addSound(sound);//铃音  
            }  
            PushNotificationManager pushManager = new PushNotificationManager();  
            //true:表示的是产品发布推送服务 false:表示的是产品测试推送服务  
            pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, certificatePassword, false));  
            List<PushedNotification> notifications = new ArrayList<PushedNotification>();  
            // 发送push消息  
            if (sendCount)  
            {  
                Device device = new BasicDevice();  
                device.setToken(tokens.get(0));  
                PushedNotification notification = pushManager.sendNotification(device, payLoad, true);  
                notifications.add(notification);  
            }  
            else  
            {  
                List<Device> device = new ArrayList<Device>();  
                for (String token : tokens)  
                {  
                    device.add(new BasicDevice(token));  
                }  
                notifications = pushManager.sendNotifications(payLoad, device);  
            }              
            pushManager.stopConnection();  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  
}









本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
IOS消息推送之APNS
iOS上简单推送通知(Push Notification)的实现
iPhone消息推送服务实现
手把手教你配置苹果APNS推送服务|钿畑的博客 | 钿畑的博客
iOS推送小结(证书的生成、客户端的开发、服务端的开发)
分分钟搞定IOS远程消息推送
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服