Google App Engine APNS

后端 未结 8 2012
陌清茗
陌清茗 2021-02-06 10:08

Im developing the server side for an iOS app with Google App Engine and JDO in Java, and I just realized that GAE dont support Apple Push Notification Service, and I`m very frus

8条回答
  •  别跟我提以往
    2021-02-06 10:44

    I use the 3rd-party library notnoop/java-apns. It is easy to use. The only problem you could meet is the thread limitation on the GAE like below java exception:

    java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "modifyThreadGroup")
    

    The issue is solved in the version 1.0.0.Beta3 in the maven central. The detail solution is explained in this pull request #162.

    So, the example code snippet to prepare and send push notification to APNs is like below, the key to solve the thread limitation is the method withErrorDetectionThreadFactory as below

    // Prepare ApnsService
    ClassPathResource certificate = new ClassPathResource("aps_production.p12");
    
    ApnsService service = null;
    try {
        service = APNS.newService()
          .withErrorDetectionThreadFactory(ThreadManager.currentRequestThreadFactory()) // use GAE currentRequestThreadFactory
          .withCert(certificate.getInputStream(), certificatePassword)
          .withProductionDestination()
          .build();
    } catch (InvalidSSLConfig | IOException e) {
        logger.warn("Fail to initialize APNs service");
    }
    
    // Send notification
    String apnsPayload = APNS.newPayload()
        .alertBody("test alert")
        .badge(1)
        .sound("default")
        .customField("type", "general")
        .build();
    
    service.push(, apnsPayload);
    

提交回复
热议问题