有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java iOS推送通知不工作

我在客户机上使用带有Ionic Native Push Notifications的离子3,在服务器上使用Java com.notnoop.apns

我可以让推送通知在Android设备上成功运行。但是,在iOS设备上,客户端不显示通知

客户端(ts)

        let topics: string[] = [this.personModelLoggedIn.uid];
        const options: PushOptions = {
          安卓: {
            senderID: "XXXXXXXXXXXXXX",
            sound: "true",
            vibrate: "true",
            topics: topics
          },
          ios: {
            alert: "true",
            badge: false,
            sound: "true"
          },
          windows: {}
        };
        const pushObject: PushObject = this.push.init(options);


        pushObject.on('notification').subscribe((data: any) => {
          alert('Received Notification!!! message = ' + data.message);
        });

服务器(java)

安卓

private String sendAndroidPushNotification(String device_token, String topics, String title, String message)
        throws Exception {
    String pushMessage = null;
    if (device_token != null && !device_token.equals("null")) {
        pushMessage = "{\"data\":{\"title\":\"" + title + "\",\"message\":\"" + message + "\"},\"to\":\""
                + device_token + "\"}";
    } else {
        pushMessage = "{\"data\":{\"title\":\"" + title + "\",\"message\":\"" + message + "\"},\"to\": \"/topics/"
                + topics + "\"}";
    }
    // Create connection to send FCM Message request.
    URL url = new URL("https://fcm.googleapis.com/fcm/send");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "key=" + SERVER_KEY);
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    // Send FCM message content.
    OutputStream outputStream = conn.getOutputStream();
    outputStream.write(pushMessage.getBytes());

    return "Android Push Notification: " + conn.getResponseCode() + " " + conn.getResponseMessage() + " - " + pushMessage;
}

iOS

private String sendIOSPushNotification(String device_token, String topics, String title, String message)
        throws Exception {
    ApnsService service = APNS.newService().withCert(PATH_TO_P12_CERT, CERT_PASSWORD).withSandboxDestination()
            .build();

    String payload = APNS.newPayload()
            // .customFields(map)
            .alertBody(title + " " + message).sound("default").build();

    //service.push(Utilities.encodeHex(topics.getBytes()), payload);
    service.push(device_token), payload);
    return "iOS Push Notification: " + title + " " + message;
}

当使用适当的devise tokens调用上述两个java方法时,Android设备接收通知,但iOS设备不接收通知

问题

如果有人能告诉我我的iOS代码(客户端或服务器)有什么问题,我将非常感谢你的建议

更新

如果我尝试在http://pushtry.com/上用我的应用程序apns-prod-cert.p12测试我的APN,我会得到以下结果:

Push Notification sent Successfully

客户端确实会显示通知。这让我觉得我的服务器代码有问题


共 (1) 个答案

  1. # 1 楼答案

    解决方案

    private String sendIOSPushNotification(String device_token, String topics, String title, String message)
            throws Exception {
        ApnsServiceBuilder serviceBuilder = APNS.newService();
            serviceBuilder.withCert(PATH_TO_P12_CERT, CERT_PASSWORD)
                    .withProductionDestination();
        ApnsService service = serviceBuilder.build();
        String payload = APNS.newPayload()
                .alertBody(message)
                .alertTitle(title)
                .sound("default")
                .customField("custom", "custom value").build();
        service.push(device_token, payload);
    
        return "iOS Push Notification: " + title + " " + message;
    }