Azure / azure-notificationhubs-java-backend

Azure Notification Hubs SDK for Java
https://docs.microsoft.com/en-us/azure/notification-hubs/
Apache License 2.0
35 stars 49 forks source link

iOS not receiving pending push after turn on Internet from Azure Notification Hub #129

Open AdaoBartolomeuAB opened 2 years ago

AdaoBartolomeuAB commented 2 years ago

I'm working on integrating Azure Notification Hub with a Java backend to send notifications of the latest news to Android and iOS devices.

In recent days I've identified the following issue, iOS devices don't receive notifications that were sent when the devices were off the internet. Notifications are lost and are not sent back to devices when the internet is turned on.

To overcome this problem I added the value of the expiration date, as you can see in the example below, but the problem persists.

    Date now = new Date();
    Date expiry = new Date(now.getTime() + 259200000L);

    NotificationHub hub = new NotificationHub("connectionString", "hubPath");

    String alert = "{\"aps\":{\"alert\":\""+notification.getBody()+"\",\"body\":\""+notification.getTitle()+"\",\"sound\":\"default\",\"id-news\":\""+notification.getIdNews()+"\"}}";
    Notification ios = Notification.createAppleNotification(alert,expiry);
    hub.sendNotification(ios);

A second option I added the ServiceBusNotification-Apns-Expiry values and apns-expiration directly in the header but the problem continues.

    Date now = new Date();
    Date expiry = new Date(now.getTime() + 259200000L);

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    String expiryString = formatter.format(expiry.getTime());

    Map<String, String> headers = new HashMap<>();
    headers.put("ServiceBusNotification-Format", "apple");
    headers.put("apns-push-type", "alert");
    headers.put("apns-priority", "10");
    headers.put("ServiceBusNotification-Apns-Expiry", expiryString);
    headers.put("apns-expiration", "259200000");

    NotificationHub hub = new NotificationHub("connectionString", "hubPath");

    String alert = "{\"aps\":{\"alert\":\""+notification.getBody()+"\",\"body\":\""+notification.getTitle()+"\",\"sound\":\"default\",\"id-news\":\""+notification.getIdNews()+"\"}}"; 
    Notification ios = Notification.createAppleNotification(alert,headers);
    hub.sendNotification(ios);