APNS PUSH用于来自一个Java / Tomcat服务器的多个应用程序ID(多个.p12证书)

时间:2016-10-09 12:55:17

标签: java apple-push-notifications javaapns

我使用notnoop库发送APNS推送。我的服务器基于java构建并托管在tomcat / apache webserver(一个静态IP)上。我在app商店有2个iphone应用程序。两者都有应用程序ID和证书。当我尝试从我们的服务器发送推送时,它只向该应用程序发送推送,该证书首次初始化。假设我有2个应用程序,AppA和AppB,他们有2个不同的应用程序ID,AppIDA和AppIDB,他们有自己的证书(.p12)CertiA和CertiB。 在我重新启动我们的tomcat服务器后,如果一个推送发起让我们说AppA,它将成功传递给设备。如果针对另一个App AppB启动了另一个推送,它将无法传送到设备。我不明白为什么会这样。这两个证书我单独测试,两者都工作正常。以下是java代码。

此代码每次为每个App AppA和AppB创建新实例。

import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;
import com.notnoop.apns.ApnsServiceBuilder;


//This is the class to send the PUSH 

public class APNSConnectionImpl extends MessagingConnection {

//APNS Builder, using notnoop library
     ApnsServiceBuilder apnsServiceBuilder = null;
//APNS Service, using notnoop library
    ApnsService apnsService = null;


// This function is use to send PUSH
// Parameters:
// deviceAndPaylodiOS : List of Devices and Payload
// appRegistrationKey : Name of the certificate, It could be AppA Certificate or //AppB Certificate. The certificate comes at runtime and both certificates copy and //past in a specific folder lat say /var/apncertificate/AppACertificate.p.12 and     /var/apncertificate/AppBCertificate.p.12


    @Async
    @Override
    public HashMap<String, String> sendMessagePool(Map<String, String> deviceAndPaylodiOS, String appRegistrationKey) 
            throws ApplicationException {

        initializeApnsServices(appRegistrationKey);

        if (deviceAndPaylodiOS == null || deviceAndPaylodiOS.isEmpty()) {
            return null;
        }

// Sending Push to all devices,
        deviceAndPaylodiOS.forEach((key, value)->{
            apnsService.push(key, value);
        });

        if (apnsService != null) {
            apnsService.stop();
            apnsService = null;
            apnsServiceBuilder = null;
        }
        return null;
    }

    private void initializeApnsServices(String appRegistrationKey) throws ApplicationException{

        if (apnsServiceBuilder != null) {
            apnsServiceBuilder = null;
        }

        apnsServiceBuilder = APNS.newService();

        try {
            String certPath = "/var/apnscertificates/"+ appRegistrationKey;
        apnsServiceBuilder.withCert(certPath,Constants.APNS_PASS_CERTIFICATE).withProductionDestination();

        } catch (Exception e) {
            e.printStackTrace();
            throw new QuinchyApplicationException("APNS certificate problem");
        }

        if (apnsService != null) {
            apnsService.stop();
            apnsService = null;
        } 

        apnsService = apnsServiceBuilder.build();
    }
}

0 个答案:

没有答案