【问题标题】:iOS Push Notifications not workingiOS推送通知不起作用
【发布时间】:2017-06-27 16:39:46
【问题描述】:

我在客户端使用带有Ionic Native Push Notifications 的Ionic 3,在服务器上使用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)

安卓

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 测试我的APNS,我会得到以下结果:

推送通知发送成功

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

【问题讨论】:

    标签: java angular ionic-framework push-notification apple-push-notifications


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 2023-03-17
      • 1970-01-01
      • 2015-06-28
      • 2019-09-22
      相关资源
      最近更新 更多