我在客户端上使用带有implicits的Ionic 3,在服务器上使用Java Ionic Native Push Notifications。
我可以使推送通知在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
代码(客户端或服务器)有什么问题,我将非常感谢您的建议。
更新
如果我尝试使用我的应用apns-prod-cert.p12
在com.notnoop.apns上测试我的APNS,我会收到以下信息:
推送通知已成功发送
客户端会显示通知。所以这让我觉得我的服务器代码出了问题。
答案 0 :(得分:0)
解决方案
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;
}