Ivan
Ivan

Reputation: 73

iOS not receiving pending push after turn on device or Internet from Azure Notification Hub

I have a Notification Hub in Azure to send PUSH to Android and iOS devices. Everything is working well in Android. In iOS I have this problem:

  1. I turn off the WiFi or I turn off the device
  2. I send chat messages to my app (push notifications) and also messages to that device from WhatsApp.
  3. I turn on the WiFi or turn on the device after a few minutes

I receive all the WhatsApp messages notifications but no one of the notification to my App. If the device and Internet is on, I receive the notifications without problem.

My question is: Anyone has experienced something like that or know how to fix it? At least I should have to receive the last push, right?

I'm sending the push to a tag. I can see the Device registered in the hub either when WiFi or iPhone are off.

Upvotes: 0

Views: 1022

Answers (2)

Sateesh
Sateesh

Reputation: 376

When sending notification ensure that you set the expiration. Default value is zero. Hence, APNs treats the notification as if it expires immediately and does not store the notification or attempt to redeliver it.

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html

apns-expiration
If this value is nonzero, APNs stores the notification and tries to deliver it at least once, repeating the attempt as needed if it is unable to deliver the notification the first time. If the value is 0, APNs treats the notification as if it expires immediately and does not store the notification or attempt to redeliver it.

If you are using template notifications, here is how you do it.

    AppleTemplateRegistrationDescription registration = new AppleTemplateRegistrationDescription(parameters.registrationID)
{ BodyTemplate = new CDataMember("{\"aps\":{\"alert\":\"$(body)\",\"payload\":\"$(payload)\",\"deeplinking\":\"$(deeplinking)\",\"category\":\"$(category)\",\"image\":\"$(image)\"}}"),
                                Tags = itags,
Expiry = "$(APNS_Expiry)"
};

As part of send, you can pass the expiry value.

var notification = new TemplateNotification(new Dictionary<string, string>()
{
 {"APNS_Expiry", DateTime.UtcNow.AddMinutes(10).ToString("o") }, //Timestamp
                        {"body", NotificationText},
                        {"payload", NotificationText},
                        {"deeplinking", payload},
                    }); 
            var Responsehub = hub.SendNotificationAsync(notification);

If you are using native notifications,

  // Native notification
    var notification = new AppleNotification(@"
    {
      ""aps"": {
        ""alert"":""New notification!""
      }
    }");

    notification.Expiry = DateTime.UtcNow.AddMinutes(2); 

await notificationHubClient.SendNotificationAsync(notification);

Upvotes: 1

Sateesh
Sateesh

Reputation: 376

If you are using template notifications, here is how you do it.

    AppleTemplateRegistrationDescription registration = new AppleTemplateRegistrationDescription(parameters.registrationID)
{ BodyTemplate = new CDataMember("{\"aps\":{\"alert\":\"$(body)\",\"payload\":\"$(payload)\",\"deeplinking\":\"$(deeplinking)\",\"category\":\"$(category)\",\"image\":\"$(image)\"}}"),
                                Tags = itags,
Expiry = "$(APNS_Expiry)"
};

As part of send, you can pass the expiry value.

var notification = new TemplateNotification(new Dictionary<string, string>()
{
 {"APNS_Expiry", DateTime.UtcNow.AddMinutes(10).ToString("o") }, //Timestamp
                        {"body", NotificationText},
                        {"payload", NotificationText},
                        {"deeplinking", payload},
                    }); 
            var Responsehub = hub.SendNotificationAsync(notification);

If you are using native notifications,

  // Native notification
    var notification = new AppleNotification(@"
    {
      ""aps"": {
        ""alert"":""New notification!""
      }
    }");

    notification.Expiry = DateTime.UtcNow.AddMinutes(2); 

await notificationHubClient.SendNotificationAsync(notification);

Upvotes: 0

Related Questions