ios推送--apns

本文详细介绍了如何在iOS上实现VOIP推送,包括申请证书、使用pushy库进行依赖配置,以及通过ApnsClient发送推送的过程。适用于希望优化推送体验,如语音通话场景的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ios推送–apns

前言

  • 使用极光推送或友盟推送在ios上只能推送普通的信息,普通推送在用户点击通知栏时这时才唤醒app,于是看到app从启动页开始加载,这对于一些场景下可能不适用,比如我想在点击时就可以马上显示想要的画面,在做语音通话时这点显得比较重要
  • 普通推送是做不到了,callkit在中国又是被禁的,那么就只剩下VOIP了
  • 但VOIP需要证书这点还是比较麻烦的,搞到证书后要通过苹果审核也只能看运气了(详见 iOS审核被拒大全:https://www.cnblogs.com/YolvinBlog/p/6296091.html)

准备

  • 向苹果申请pc12格式的证书(一年有效,到期需重新申请)

步骤

  • 本次使用的github上的库pushy(另外一个库java-apns据说在jdk8上有问题,因此没使用)

添加依赖

  • 以下以gradle方式示例

    	// https://mvnrepository.com/artifact/com.turo/pushy
        compile group: 'com.turo', name: 'pushy', version: '0.13.7'
    

获得ApnsClient

  • 通过设置证书和密码获得ApnsClient对象

    //证书路径
    Resource resource = new ClassPathResource("static/ACS_Landlord_VoIP_2019-03-14.p12");
    File file = resource.getFile();
    final ApnsClient apnsClient = new ApnsClientBuilder()
                  .setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST) //生产环境还是开发环境
                  .setClientCredentials(file, "123456") //证书密码
                  .build();
    
  • 其中ApnsClientBuilder.DEVELOPMENT_APNS_HOST表示在测试环境中使用,假如ios使用的是生产环境,那么则要改为ApnsClientBuilder.PRODUCTION_APNS_HOST,否则推送会失败

推送

  • 获得设备码后开始推送
final SimpleApnsPushNotification pushNotification;

        final String token = TokenUtil.sanitizeTokenString("9e3a8da155d97e14ab459923d8992bf48e2807a4defb9517d6534311c82c1d57");

        pushNotification = new SimpleApnsPushNotification(token, "com.xxx.xxx.voip", "这是推送内容");

        final PushNotificationFuture<SimpleApnsPushNotification, PushNotificationResponse<SimpleApnsPushNotification>>
                sendNotificationFuture =  apnsClient.sendNotification(pushNotification);
        try {
            final PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse =
                    sendNotificationFuture.get();

            if (pushNotificationResponse.isAccepted()) {
                System.out.println("Push notification accepted by APNs gateway.");
            } else {
                System.out.println("Notification rejected by the APNs gateway: " +
                        pushNotificationResponse.getRejectionReason());

                if (pushNotificationResponse.getTokenInvalidationTimestamp() != null) {
                    System.out.println("\t…and the token is invalid as of " +
                            pushNotificationResponse.getTokenInvalidationTimestamp());
                }
            }
        } catch (final ExecutionException e) {
            System.err.println("Failed to send push notification.");
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

参考

relayrides/pushy: A Java library for sending APNs (iOS/macOS/Safari) push notifications
https://github.com/relayrides/pushy
notnoop/java-apns: Java Apple Push Notification Service Provider
https://github.com/notnoop/java-apns

Apple APNs java client, based on netty4. 基于netty4实现的苹果通知推送服务Java客户端。 特点: 支持第三版通知推送,即command = 2。目前的绝大部分Java客户端都只支持command = 1,即第二版。 支持SSL握手成功才返回,可以调用 pushManager.start().sync(); 等待握手成功才开始发送; 最大限度重试发送,内部自动处理重连,错误重发机制; 支持配置RejectListener,即通知被Apple服务器拒绝之后的回调接口; 支持配置ShutdownListener,即当shutdown时,没有发送完的消息处理的回调接口; 支持发送统计信息; 实现组件分离,可以利用PushClient,FeedbackClient来写一些灵活的代码。 Notification发送者可以自己定义设置发送的Queue,自己灵活处理阻塞,超时等问题。     Example: 更多的例子在src/test/java 目录下。 public class MainExample {     public static void main(String[] args) throws InterruptedException {         Environment environment = Environment.Product;         String password = "123456";         String keystore = "/home/hengyunabc/test/apptype/app_type_1/productAPNS.p12";         PushManager pushManager = new PushManagerImpl(keystore, password, environment);         //set a push queue         BlockingQueuequeue = new LinkedBlockingQueue(8192);         pushManager.setQueue(queue );         //waiting for SSL handshake success         pushManager.start().sync();         //build a notification         String token = "5f6aa01d8e3358949b7c25d461bb78ad740f4707462c7eafbebcf74fa5ddb387";         Notification notification = new NotificationBuilder()                 .setToken(token)                 .setBadge(1)                 .setPriority(5)                 .setAlertBody("xxxxx").build();         //put notification into the queue         queue.put(notification);         TimeUnit.SECONDS.sleep(10);         //get statistic info         Statistic statistic = pushManager.getStatistic();         System.out.println(statistic);     } } 标签:zpush
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值