繁体   English   中英

iOS推送通知不起作用

[英]iOS Push Notifications not working

我在客户端上使用Ionic 3和Ionic Native Push Notifications ,在服务器上使用Java com.notnoop.apns

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

客户 (ts)

        let topics: string[] = [this.personModelLoggedIn.uid];
        const options: PushOptions = {
          android: {
            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)

Android的

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代码(客户端或服务器)有什么问题,我将非常感谢您的建议。

UPDATE

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

推送通知已成功发送

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

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM