Open santosh-1987 opened 8 years ago
Hi, i've tried a lot of time to uninstall reinstall application... After unistall and reinstall, my BackEnd server receive different token from the same device and i'm able to send notification. Notification is diplayed on my phone... yeahhhhhh.... But i've no clean device token table on my DB... so...
I unistall the application... In my BE table there are lot of deviceToken that i want to delete, but when i call service.getInactiveDevices(); the library return always empty map...
This is a bug or i don't understand how it works ?
Thanks...
Hi found the problem. The feedback arrive after service is already started.. I write this class based on other found on internet. The different on the other class is that APNSservice is instanziated only one time, then elaborate all push notification and then stop...
Below my class:
import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;
import com.notnoop.apns.APNS; import com.notnoop.apns.ApnsService; import com.notnoop.apns.ApnsServiceBuilder; import com.notnoop.apns.PayloadBuilder;
@Service @Qualifier("Apns") public class ApnsPushNotificationService { private File cert; private String certPassword; private boolean useProductionServer; private String serverAddress; private static final int DEFAULT_APNS_PORT = 2195;
private ApnsService apns = null;
/**
* @param cert
* @param certPwd
* @param useProductionServer
* @param serverAddress
*/
public ApnsPushNotificationService(File cert, String certPwd, boolean useProductionServer, String serverAddress) {
this.cert = cert;
this.certPassword = certPwd;
this.useProductionServer = useProductionServer;
this.serverAddress = serverAddress;
}
public void init() throws Exception {
if(apns == null) {
ApnsServiceBuilder serviceBuilder = APNS.newService().withCert(new FileInputStream(cert), certPassword);
if (null != serverAddress && serverAddress.length() > 0) {
serviceBuilder.withGatewayDestination(serverAddress, DEFAULT_APNS_PORT);
} else if (useProductionServer) {
serviceBuilder.withProductionDestination();
} else {
serviceBuilder.withSandboxDestination();
}
this.apns = serviceBuilder.build();
}
}
public void startService() {
if(apns != null)
apns.start();
}
public void stopService() {
if(apns != null)
apns.stop();
}
public void pushNotification(Notification notification) throws Exception {
List<Notification> notList = new ArrayList<Notification>();
notList.add(notification);
pushNotifications(notList);
}
public void pushNotifications(List<Notification> notifications) throws Exception {
for (Notification notification : notifications) {
doSendNotification(notification);
}
}
private void doSendNotification(Notification notification) throws Exception {
PayloadBuilder payloadBuilder = APNS.newPayload();
if(notification.getBadge() != null)
payloadBuilder = payloadBuilder.badge(notification.getBadge());
if(notification.getSound() != null)
payloadBuilder = payloadBuilder.sound(notification.getSound());
if (notification.getBody() != null) {
payloadBuilder = payloadBuilder.alertBody(notification.getBody());
}
String payload = payloadBuilder.build();
apns.push(notification.getDeviceToken(), payload);
}
public Map<String, Date> getDeviceTokenToDelete() throws Exception {
return apns.getInactiveDevices();
}
}
Example of use:
ApnsPushNotificationService apns = new ApnsPushNotificationService(new File("absolutFilePathOfCert"), "passwordOfCert", false, null); apns.init(); apns.startService(); iterate on saved device token; deviceTokenMaps = apns.getDeviceTokenToDelete(); apns.stop(); iterate on deviceTokenMaps keyset and perform DB delete;
Hope this helpful. Bye
Hi Team,
Is there any change in APNS feedback service , which returns inactive device token that is to be removed from devices table/database.
AND
Is there a way to compare the devices list present in DB with APNS server and remove the inactive devices.
Any Help would be greatly appreciated.