0

我正在使用 notnoop 库来发送 APNS 推送。我的服务器基于 java 构建并托管在 tomcat/apache 网络服务器(一个静态 IP)上。我有 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();
    }
}
4

0 回答 0