您的位置:首页 > 移动开发 > Swift

iOS 远程推送 (swift +java后台)

2016-11-10 09:37 435 查看

首先吐槽下 苹果的网站,卡得要死进个网页几十分钟!

    1.找到钥匙串访问->证书助手->从证书颁发机构请求证书,填写电子邮件和地址然后   保存到磁盘   得到  CertificateSigningRequest.certSigningRequest。



2.创建app ID  



勾选 push notifications  



   create certification



   选择上一部生成的的  密钥    CertificateSigningRequest.certSigningRequest。 下一步生成 证书  aps_development.cer

双击证书 安装 


找到刚刚安装的证书   右键导出  选择 p12 类型 然后 输入密码       这个 文件 在 java 上要用到

然后 找到provisioning Profiles
  生成   调试证书       appid  填写 正确     选择好 调试的设备  导出


xinjiaxiao.mobileprovision

双击  安装  

code  新建项目


bundle  identifiter  写正确







  打开远程通知  和允许后台通知   



配置  provisioning  profile    

iOS  代码   注册推送

[objc] view
plain copy

 





func registerForPushNotifications(application: UIApplication) {  

       print("注册推送")  

      let notificationSettings = UIUserNotificationSettings(  

          forTypes: [.Badge, .Sound, .Alert], categories: nil)  

      application.registerUserNotificationSettings(notificationSettings)  

  }  

注册成功  输出 token

[objc] view
plain copy

 





func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {  

       let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)  

       var tokenString = ""  

         

       for i in 0..<deviceToken.length {  

           tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])  

       }  

         

       print("Device Token:", tokenString)  

   }  

失败  

[objc] view
plain copy

 





func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {  

    print("Failed to register:", error)  

}  

收到推送的处理

[objc] view
plain copy

 





func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {  

      print("saasasas",userInfo)  

  }  

java  后台  

[java] view
plain copy

 





package kkkk;  

  

  

  

import java.util.ArrayList;    

import java.util.List;    

    

    

    

    

import org.apache.commons.lang.StringUtils;    

    

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 AA    

{    

    public static void main(String[] args) throws Exception    

    {    

        String deviceToken = "4042257157b535b3ac2fb9cf35dadadaadaa26855377e3c18da10feb2";    

        String alert = "收到通知了";//push的内容    

        int badge = 3;//图标小红圈的数值    

        String sound = "default";//铃音    

    

        List<String> tokens = new ArrayList<String>();    

        tokens.add(deviceToken);    

        String certificatePath = "D:/push.p12";       

        String certificatePassword = "hzy001ss'019";//此处注意导出的证书密码不能为空因为空密码会报错    

        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>();    

              

            System.out.println("dadsadsadadada");  

            // 发送push消息    

            if (sendCount)    

            {    

                Device device = new BasicDevice();    

                device.setToken(tokens.get(0));    

                PushedNotification notification = pushManager.sendNotification(device, payLoad, true);    

                notifications.add(notification);    

                System.out.println("发生");  

            }    

            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)    

        {    System.out.println("AAAAAAAAAAAAAAAAAAAAAA");  

            e.printStackTrace();    

        }    

    }    

}    

   

 代码很简单  主要是几个证书的  配置 麻烦  ,还有 苹果开发者中心 网太慢 !!  遇到的问题  服务端显示成功  而ios 未收到通知  ,检查了下 是 证书配置问题

打开 xode -> preferences  accounts  找到开发者账号下 

   


点设置  export deceloper accounts  导出账号证书  双击安装

然后 view details    删除无用的证书  下载 需要的那一个证书  安装  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: