Hi All,
I am using Ionic 3 with Ionic Native Push Notifications on the client and Java com.notnoop.apns on the server.
I can get the Push Notifications to work successfully on an Android
devise. However, on an iOS
devise, the client does not display the notification.
client (ts)
let topics: string[] = [this.personModelLoggedIn.uid];
const options: PushOptions = {
android: {
senderID: "893141127008",
sound: "true",
vibrate: "true",
topics: topics
},
ios: {
senderID: "893141127008",
alert: "true",
badge: true,
sound: "true"//,
//topics: topics
},
windows: {}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((data: any) => {
alert('Received Notification!!! message = ' + data.message);
});
server (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(Utilities.encodeHex(device_token.getBytes()), payload);
return "iOS Push Notification: " + title + " " + message;
}
When the above two java methods get called with the appropriate devise tokens
, the Android
devise receives a notification, but the iOS
devise does not.
If anyone can advise what’s wrong with my iOS
code (either client or server), I would appreciate your advise.