Giter VIP home page Giter VIP logo

Comments (60)

marstr avatar marstr commented on June 2, 2024 4

Howdy folks, we've found some server side logic that was getting in the way of including these headers. We're working on a fix, and will update this thread when we have deployed the changes.

from azure-notificationhubs-dotnet.

marstr avatar marstr commented on June 2, 2024 2

Howdy folks, I've just confirmed that we've finished rolling out our changes to infer the apns-push-type header value globally. Furthermore, I can confirm that we're seeing the expected behavior on our side. Please confirm whether or not you are still impacted.

However, there are still issues at play that may be preventing your notifications from going through! For instance, make sure that you're setting the apns-priority header appropriately as documented by Apple: Sending Notification Requests to APNs

As I hear back from you on whether or not you're still impacted, I will split out any related issues into their own issues as appropriate.

@neil445 if you believe Notification Hubs isn't delivering your notifications, please reach-out to Azure Support to begin an investigation.

@anthonyplews
@AntRemo
@gertgjoka
@JohnBergman
@milettal
@neil445

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024 1

@marstr I understand and thanks for the update :)

from azure-notificationhubs-dotnet.

JohnBergman avatar JohnBergman commented on June 2, 2024 1

Updating our logic to match @milettal notes above did not change the behavior. We see the device notification, and even using the Azure Tool have been unable to get a successful background push received in the app. This was working prior to the change.

@marstr Any update on deploying a resolution to the issue?

from azure-notificationhubs-dotnet.

anthonyplews avatar anthonyplews commented on June 2, 2024 1

Also waiting for an update on this?

from azure-notificationhubs-dotnet.

marstr avatar marstr commented on June 2, 2024 1

Howdy folks, we're still working on repro'ing this problem. To help us get to the bottom of it, would you mind confirming which version of iOS 13 you're using?

@AntRemo
@gertgjoka
@JohnBergman
@neil445
@ostastny

from azure-notificationhubs-dotnet.

marstr avatar marstr commented on June 2, 2024 1

Howdy @AntRemo,

Your frustration is understood! The original problem was that InstallationTemplates targeting APNs with any Headers were blocked by some of our server side validation. We've since deployed a fix for that, and have validated that we're passing the appropriate headers through our system.

Since that fix, we're still hearing from some folks on this thread that background notifications are not being delivered to their applications. We're taking that seriously, and have traced through our system to make sure that we're delivering notifications to Apple in the format we had expected. At the moment, we believe we are delivering notifications as appropriate. However, we're still in the process of doing due diligence before we can make any conclusions about the behavior reported here.

Sorry for the inconvenience!

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024 1

Hi @RajGogri

I just tested scenario #4 by simply swiping home and it worked properly.

I then performed the same scenario, but then opened another app and it did not appear to immediately deliver the notification to the app.

Having said that, I'm not sure if this is the normal behavior for iOS. If an app is both in the background and another app is in the foreground, it may throttle the background notification delivery speed.

Hope that helps.

from azure-notificationhubs-dotnet.

neil445 avatar neil445 commented on June 2, 2024 1

I got it working already like three weeks ago.

This might help some guys here.

I'm building a native iOS app using Objective-C. I just followed the tutorial how to do it. Here's what I've found out:

  1. If the app installed on the iPhone via XCode, the Azure Notification Hub must be in Sandbox mode for it to work.
  2. If the app is installed via TestFlight/AppStore, obviously, the Azure Notification Hub must be in Production Mode
  3. We switched Azure Notification Hub APNS configuration to Token Authentication as required by Apple.
  4. If you're not going for a silent/background notification. Make sure the apns-push-type header is set to "alert".

Here's a simple and stupid version of our code that sends the notification

var tags = new List { "user-tag-here" };
var apnsHeaders = new Dictionary<string, string>
{
{ "apns-push-type", "alert" },
{ "apns-priority", "5" }
};

            var appleNotification = new AppleNotification("{\"aps\":{ \"content-available\": 1, \"alert\":\"" + "Your custom message here" + "\" }}", apnsHeaders);

            var send = hub.SendNotificationAsync(appleNotification, tags);

from azure-notificationhubs-dotnet.

tanujnarula avatar tanujnarula commented on June 2, 2024 1

deviceToken

got it, I need to do this in native/xamarin ios app. I tried to leave them un-touched and just work with "{length=32,bytes=0xc125ed785e0a827d5957983ffa87436d...bc8fe979b25e8a6b}" on the back-end using the code(ToHexString() function) you mentioned. Let me try the one you suggested and I will provide a detailed feedback for someone to reuse the solution.

from azure-notificationhubs-dotnet.

milettal avatar milettal commented on June 2, 2024

I am having this same issue. I am using the AppleTemplateRegistrationDescription to register my template with Azure. I set the ApnsHeaders for my silent/background push to the following per this article's recommendations:

template.ApnsHeaders.Add("apns-push-type","background");
template.ApnsHeaders.Add("apns-topic","com.company.AppNameHere");
template.ApnsHeaders.Add("apns-priority","5");

Even with registering with these settings, the background pushes do not get received by the device. If I include no ApnsHeaders, I get the same result. Alerts appear to be working just fine. Any help would be appreciated as this is breaking functionality of our application on iOS 13. Thanks!

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

From azure support, the headers don't need to be on the installation registration but on the actual sending of the push message:
var backgroundHeaders = new Dictionary<string, string> { { "apns-push-type", "background" }, { "apns-priority", "5" } };
Dictionary<string, string> templateParams = new Dictionary<string, string>();
// populated templateParams according to our logic

var notification = new TemplateNotification(templateParams);
notification.Headers = backgroundHeaders;
// the second parameter is the tag name and the template name as we have it registered from the device.
var resBack = await hubClient.SendNotificationAsync(notification, tags);

Also the push hub needs to be configured with a key not a certificate to support it. I have yet to test this, but thought I would share it.

Cheers

from azure-notificationhubs-dotnet.

milettal avatar milettal commented on June 2, 2024

@gretgjoka
Interesting... Mine started working today randomly. I figured the people at Azure had something to do with it. I included the headers on the template registration, not on the sending, and it still worked. Hope it's fixed for everyone. Thanks all!

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

As an update, I just tested and mine are still not working.

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

I tested as well, not working on my side either with the changes I posted above

from azure-notificationhubs-dotnet.

milettal avatar milettal commented on June 2, 2024

@AntRemo @gertgjoka
Did you all try adding the APNS headers to the template registration as I did? It is worth a shot if you have not.

template.ApnsHeaders.Add("apns-push-type","background");
template.ApnsHeaders.Add("apns-topic","com.company.AppNameHere");
template.ApnsHeaders.Add("apns-priority","5");

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

@milettal I am using the installation method with templates, in the API i get an Installation object from the device, and it contains a list of InstallationTemplate which only has a header property. Where would the ApnsHeaders be?

from azure-notificationhubs-dotnet.

milettal avatar milettal commented on June 2, 2024

@gertgjoka Ah, I am probably using a different method than you. I am using the "Microsoft.Azure.NotificationHubs" nuget package for .NET (the GitHub we're on). With that, you can specify the Apns headers. My code looks like this:

AppleTemplateRegistrationDescription template = new AppleTemplateRegistrationDescription(deviceTokenStr);
template.Tags = new HashSet<string> { "Tag1","Tag2" };
template.ApnsHeaders=new ApnsHeaderCollection();
template.ApnsHeaders.Add("apns-push-type","background");
template.ApnsHeaders.Add("apns-topic","com.company.app");
template.ApnsHeaders.Add("apns-priority","5");
template.TemplateName = "templateName";
template.BodyTemplate = "Put JSON here with content-available: 1";
await client.CreateRegistrationAsync(template);

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

Hi @milettal

Thanks for your suggestion. You are correct, I at least, am using a different method.

I am using the InstallationTemplate class with the following APNS template

{ "aps": { "content-available": 1, "DataMessage": "$(DataMessage)" } }
This is then assigned to the Installation class which has a Templates Dictionary for InstallationTemplates.

After that is setup I call hubClient.CreateOrUpdateInstallation

from azure-notificationhubs-dotnet.

milettal avatar milettal commented on June 2, 2024

@AntRemo @gertgjoka Ah, I was unaware of the InstallationTemplate method of registering for templates. It's odd to me that this is the recommended method from Azure but you are not able to specify APNS headers... Would switching to the Registration method break your push infrastructure?

@marstr Is there any way for people using the InstallationTemplate method to set any APNS headers to be passed to Apple?

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

Hi @marstr

Just wanted to check-in and see if there was any time-frame when this issue may be resolved.

Thanks๐Ÿ™

from azure-notificationhubs-dotnet.

marstr avatar marstr commented on June 2, 2024

@milettal says:
@marstr Is there any way for people using the InstallationTemplate method to set any APNS headers to be passed to Apple?

Not as far as I'm aware, but I'm still fairly new to the team.

@AntRemo says:
Just wanted to check-in and see if there was any time-frame when this issue may be resolved.

I'm afraid I don't have much status to share! We're still in the process of rolling out our fix.

from azure-notificationhubs-dotnet.

neil445 avatar neil445 commented on June 2, 2024

seriously waiting for an update on this.

We have our two environments, having exactly the same code, but using their respective notifications hubs.

on environment A, the behaviour is not consistent. The devices (iPhone, iPad) don't receive push notifications all the time.

on environment B, it's a total outage. No push notifications are received.

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

@marstr Thanks for the great news. Would you mind telling us where specifically to set these headers?Azure support told us to set those when sending each push message, while here we saw users setting those during the device registration with Azure Hub. Thank you!

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

I just tried now setting headers in both sending of the push and registration, none of the tests worked for background push...

from azure-notificationhubs-dotnet.

ostastny avatar ostastny commented on June 2, 2024

I still observe the issue as well. Tried to update my code last week to explicitly include apns-push-type but even with that, the background notifications do not seem to work.

var notification = new AppleNotification(@"
    {
      ""aps"": {
        ""content-available"": 1
      }
    }")
    {
      Priority = 5
    };
    notification.Headers["apns-push-type"] = "background"; 

from azure-notificationhubs-dotnet.

marstr avatar marstr commented on June 2, 2024

@milettal says:
Is there any way for people using the InstallationTemplate method to set any APNS headers to be passed to Apple?

I've just verified that I'm receiving the expected Notifications on iOS 13 with the following code using Installation Templates and set headers:

private static async Task<string> SendAfterTemplateInstallation(NotificationHubClient nhClient, string deviceToken)
{
    var parity = Guid.NewGuid();
    var installTemplate = new InstallationTemplate()
    {
        Body = $"{{\"aps\":{{\"alert\":\"Notification Hub test notification with secret {parity.ToString()}\"}}}}",
        // Worth noting, I'm seeing success with and without the Headers being set below.
        Headers = new Dictionary<string, string>
        {
             {"apns-push-type", "alert"},
             {"apns-priority", "10"},
        }, 
    };

    var randomTag = Guid.NewGuid();
    var myTags = new[] { randomTag.ToString()};
            
    var installationId = Guid.NewGuid().ToString();
           
    var myInstallation = new Installation
    {
        InstallationId = installationId,
        Tags = myTags,
        Platform = NotificationPlatform.Apns,
        PushChannel = deviceToken,
        Templates = new Dictionary<string, InstallationTemplate>
        {
            {"myTemplate", installTemplate}
        }
    };

    await nhClient.CreateOrUpdateInstallationAsync(myInstallation);
    Console.WriteLine($"Installation Created with secret {parity}");

    // When I don't delay, this doesn't work. It does take some time for the system to complete 
    // an installation after it's been requested. I haven't tried pushing those limits
    for (var i = 15; i > 0; i--)
    {
        Console.WriteLine(i);
        await Task.Delay(TimeSpan.FromSeconds(1));
    }

    var result = await nhClient.SendTemplateNotificationAsync(new Dictionary<string, string>(), myTags);
    return result.TrackingId;
}

@gertgjoka, I think the above should work for you as well.
@ostastny, I suspect your issue is unrelated. Make sure you have your hands on the correct Device Token, have configured your APNS credentials correctly.

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

@marstr Your example refers to an alert notification, we are all instead saying that we don't receive background notifications, so with apns-push-type: "background", priority 5 and and content-available: 1. Can you please let us know?

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

@marstr

I too can confirm that setting the InstallationTemplate.Headers as follows does not appear to have any affect.

installationTemplate.Headers = new ApnsHeaderCollection()
                    {
                        {"apns-push-type", "background"},
                        {"apns-priority","5" }
                    };

I also reinstalled the app on my phone, just to be sure there was not an issue with the existing installation.

from azure-notificationhubs-dotnet.

marstr avatar marstr commented on June 2, 2024

@AntRemo, in the other thread you were reporting that the you got the following error message when setting the Headers property of an InstallationTemplate:

Template 'hello is invalid: 'Headers' only acceptable for WNS and MPNS...

Are you still encountering that?

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

@marstr

Correct. However, now I do not receive an error message and it appears to upload properly.

Please let me know if you would like me to add some query logic to verify it is being uploaded properly to Notification Hubs.

from azure-notificationhubs-dotnet.

ostastny avatar ostastny commented on June 2, 2024

@marstr To answer your comment, I don't believe my issue is related Device Tokens. I can successfully receive alert notifications on my test devices, it's only background notifications that don't work.
Actually, had some users report they saw some background notifications come through but even for those users the delivery success rate would be very low (one in ten or less...)

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

13.1.2

from azure-notificationhubs-dotnet.

anthonyplews avatar anthonyplews commented on June 2, 2024

13.1.2

from azure-notificationhubs-dotnet.

JohnBergman avatar JohnBergman commented on June 2, 2024

13.1.2

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

Was 13.1.2 now 13.1.3

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

I was just wondering what is your app development platform guys? We are using cordova and phonegap-push-plugin. Thanks!
@AntRemo
@neil445
@ostastny
@anthonyplews

from azure-notificationhubs-dotnet.

anthonyplews avatar anthonyplews commented on June 2, 2024

Okay so managed to get everything working:

Backend API (.net core 2.2):

  • Update API to reference the latest azure-notificationhubs-dotnet package

  • Update Device Registration method on API to convert handle to hex string:

      public async Task<DeviceRegistration> Register(string handle = null)
      {
          handle = ToHexString(handle);
          string newRegistrationId = null;
    
          if (handle != null)
          {
              var registrations = await _Hub.GetRegistrationsByChannelAsync(handle, 100);
    
              foreach (RegistrationDescription registration in registrations)
              {
                  if (newRegistrationId == null)
                  {
                      newRegistrationId = registration.RegistrationId;
                  }
                  else
                  {
                      await _Hub.DeleteRegistrationAsync(registration);
                  }
              }
          }
    
          if (newRegistrationId == null) 
         {
              newRegistrationId = await _Hub.CreateRegistrationIdAsync();
         }
    
          var deviceRegistration = new DeviceRegistration
          {
              RegistrationId = newRegistrationId
          };
    
          return deviceRegistration;
      }
    
      public async Task<DeviceRegistration> UpdateRegistration(DeviceRegistration deviceUpdate, string registrationId, string[] tags)
      {
          RegistrationDescription registration;
          switch (deviceUpdate.Platform)
          {
              case "mpns":
                  registration = new MpnsRegistrationDescription(deviceUpdate.Handle);
                  break;
              case "wns":
                  registration = new WindowsRegistrationDescription(deviceUpdate.Handle);
                  break;
              case "apns":
                  registration = new AppleRegistrationDescription(deviceUpdate.Handle);
                  break;
              case "gcm":
                  registration = new FcmRegistrationDescription(deviceUpdate.Handle);
                  break;
              default:
                  throw new Exception("Bad Request");
          }
    
          registration.RegistrationId = registrationId;
          registration.Tags = new HashSet<string>(tags);
    
          await _Hub.CreateOrUpdateRegistrationAsync(registration);
    
          return deviceUpdate;
      }
    
      private static string ToHexString(string str)
      {
          var sb = new StringBuilder();
    
          var bytes = Encoding.Unicode.GetBytes(str);
          foreach (var t in bytes)
          {
              sb.Append(t.ToString("X2"));
          }
    
          return sb.ToString();
      }
    

Mobile App (Ionic 3 Cordova):
update phonegap-plugin-push to latest version: 2.3.0.

Mobile App (Ionic 4 Capacitor)
We didn't have to do anything it worked after the backend API was updated.

Hope this helps!

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

@anthonyplews with the latest plugin, the handle is already in the right format. I had updated it already, and I am receiving alert push notifications, but not receiving the silent/background ones.
Were you receiving anything prior to this change?

from azure-notificationhubs-dotnet.

anthonyplews avatar anthonyplews commented on June 2, 2024

@gertgjoka I wasn't receiving any backend notifications prior to this change, this is a snippet of the code we use for iOS backend notifications:

 var iMsg = JsonConvert.SerializeObject(iosMessage);
 outcome = await _Hub.SendAppleNativeNotificationAsync(iMsg, tags);

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

@anthonyplews your issue was different from ours then. All of us have issues with background notifications (those with "content-available": 1). Can you receive those?
How is your hub configured, with a certificate or a key?

from azure-notificationhubs-dotnet.

anthonyplews avatar anthonyplews commented on June 2, 2024

@gertgjoka we configured a certificate.

I see your issue now, as you can see from our code we pass through a json payload and don't specify the apns-priority so it defaults to 10:

image

We haven't tested with "content-available": 1

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

Hi @gertgjoka

I am using Xamarin.

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

I was able to find a workaround for now, add the badge:0 to the apns payload for silent push and also make sure to remove the headers you may send from .net code during the push send (those were blocking the push for me). I can now finally receive a silent push
@AntRemo
@neil445
@ostastny
@anthonyplews

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

The side effect of it is that setting the badge to 0 will clear the visible/alert push messages that were there before it. Might not be an acceptable solution for everybody.

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

@gertgjoka Thanks for the update.

That will not be an option for our scenario.

Hope it has provided some relief for you.

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

Hi @marstr

Just wanted to check the status.

It is has been 8 weeks since the original issue was reported and this is beginning to be a real problem for us.

Many thanks ๐Ÿ™

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

@marstr

I have great news. I managed to get notifications to work without changing any server side code.

To be clear, I did not need to add the following to my server side.

installationTemplate.Headers = new ApnsHeaderCollection()
                    {
                        {"apns-push-type", "background"},
                        {"apns-priority","5" }
                    };

I stumbled across the following doc titled Azure Notification Hubs updates for iOS 13 which states

Developers must now set this header in applications that send notifications through Azure Notification Hubs. Due to a technical limitation, customers must use token-based authentication for APNS credentials with requests that include this attribute. If you are using certificate-based authentication for your APNS credentials, you must switch to using token-based authentication.

Because I have been using Notifications Hubs for years, I had it setup to use certificate-based authentication.

After changing it to token-based authentication following this document Token-based (HTTP/2) Authentication for APNS It worked! Also because no code changed, I was able to update and confirm production is working without doing a deployment.

Question
Should I add the code above to set the headers or is that an optional step because I am setting "content-available": 1 in the payload?

Note for others attempting to switch from certificate to token:
It took me a few mins to figure out what the token was obtained. I simply had to open the .p8 file I downloaded and extract the characters between the -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- and paste that into the Token field.

from azure-notificationhubs-dotnet.

gertgjoka avatar gertgjoka commented on June 2, 2024

@AntRemo I have mentioned on my October 3rd comment that you need a token according to what Azure support told me, but it didn't solve the issue for silent push notifications on my case.
Can you please confirm if you are now receiving background/silent push notifications?

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

@gertgjoka I must have missed that. ๐Ÿ™„๐Ÿ˜‚

I can 100% confirm it is working for me now, with no changes to server code.

The only change I made to my app, a month ago, was updating the way that the device token is extracted for iOS 13

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
                ...

                if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
                {
                    // New for iOS 13
                    byte[] result = new byte[deviceToken.Length];
                    Marshal.Copy(deviceToken.Bytes, result, 0, (int)deviceToken.Length);
                    pushChannel = BitConverter.ToString(result).Replace("-", "");
                }
                else
                {
                    // Prior to iOS 13
                    pushChannel = deviceToken.Description.Trim('<').Trim('>').Replace(" ", "");
                }

                ...
}

It may be worth double checking the Notification Hub settings on the Azure portal.

Please let me know if you have any other questions.

Good Luck!
Ant

from azure-notificationhubs-dotnet.

RajGogri avatar RajGogri commented on June 2, 2024

Hi Guys!
I am a Xamarin developer and following this thread for a while. I managed to receive silent push notifications on my ios app but I see some strange behavior as follows;

  1. Silent push works when the app is in the foreground and connected to wifi.
  2. Silent push works when the app is in the background and connected to wifi.
  3. Silent push works when the app is in the foreground and connected to a cellular network.
    But here's the real problem for me.
  4. Silent push Doesn't work when the app is in the background and connected to a cellular network.

I am not using any apns headers instead, I am just using payload for sending silent push.

@marstr @gertgjoka @AntRemo @anthonyplews @neil445 @ostastny can you pls confirm are you able to see this behavior on your side as well?

or is there anything that I am missing from my side?

from azure-notificationhubs-dotnet.

RajGogri avatar RajGogri commented on June 2, 2024

Thanks @AntRemo for quick response. I tested in the way u suggested by swiping home, but unfortunately no signs of receipt of silent push for me. Did you performed your test by connecting to cellular network ? I asked that because for me silent apns push works fine when phone is connected to wifi and app is in background.

Also now i have changed my backend code to send required apns headers along with payload in azure notification hub using AppleNotification class. But still no success. ๐Ÿ˜”

On my further testing what I found is;
The issue mentioned in point 4 #96 (comment) here doesnโ€™t occur on pre iOS 13 devices.

So basically, Silent Apns Push is not working for me when app is in background and connected to cellular network on iOS 13 devices.

Need assistance!!!

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

@RajGogri I tested on iPhone 11 Pro with iOS 13.2 with wifi off and cell network on.

Then I put the phone in airplane mode and returned to the app and found data was updated.

from azure-notificationhubs-dotnet.

RajGogri avatar RajGogri commented on June 2, 2024

Ok @AntRemo Can you please share the configuration you have done for ios app to receive silent push as well as backend configuration to send silent push.
That would really helpful for me.
Thanks

from azure-notificationhubs-dotnet.

RajGogri avatar RajGogri commented on June 2, 2024

@neil445 Thanks for your response. I have the same configuration as yours but instead of alert push-type, I am using background/silent notification push-type. So my code to send push looks like;

var tags = new List { "user-tag-here" };
var apnsHeaders = new Dictionary<string, string>
{
{ "apns-push-type", "background" },
{ "apns-priority", "5" }
};

var appleNotification = new AppleNotification("{"aps":{ "content-available": 1, "msg":"silent push notification"}}", apnsHeaders);

var send = hub.SendNotificationAsync(appleNotification, tags);

But using I face issue mentioned
The issue mentioned in point 4 #96 (comment) here and it doesnโ€™t occur on pre-iOS 13 devices.

Can you assist in this?

from azure-notificationhubs-dotnet.

neil445 avatar neil445 commented on June 2, 2024

@RajGogri not really sure. but have you tried setting apns-priority to 10?

just for the sake of trying, do this

var apnsHeaders = new Dictionary<string, string>
{
{ "apns-push-type", "background" },
{ "apns-priority", "5" },
{"content-available", "1"}
};

See this

from azure-notificationhubs-dotnet.

RajGogri avatar RajGogri commented on June 2, 2024

@neil445 I tried setting the priority to 10 but that too didn't work. I see the same behavior as mentioned. Thanks for your response.

from azure-notificationhubs-dotnet.

marstr avatar marstr commented on June 2, 2024

Thanks for all of the valuable discussion here, folks! I've been working with open source repositories for a long time, and it's truly rare to see this level of community engagement and help. Big props :)

I'm happy to continue to leave this thread unlocked for discussion, but the original need for this thread has been fulfilled. (The server was rejecting installations against APNS that included headers.) To keep our backlog clean, I'm going to close this issue. If you identify any other actionable need from them team, feel free to open a new issue.

Thanks again for everybody's sustained attention and patience.

from azure-notificationhubs-dotnet.

tanujnarula avatar tanujnarula commented on June 2, 2024

Okay so managed to get everything working:

Backend API (.net core 2.2):

  • Update API to reference the latest azure-notificationhubs-dotnet package
  • Update Device Registration method on API to convert handle to hex string:
      public async Task<DeviceRegistration> Register(string handle = null)
      {
          handle = ToHexString(handle);
          string newRegistrationId = null;
    
          if (handle != null)
          {
              var registrations = await _Hub.GetRegistrationsByChannelAsync(handle, 100);
    
              foreach (RegistrationDescription registration in registrations)
              {
                  if (newRegistrationId == null)
                  {
                      newRegistrationId = registration.RegistrationId;
                  }
                  else
                  {
                      await _Hub.DeleteRegistrationAsync(registration);
                  }
              }
          }
    
          if (newRegistrationId == null) 
         {
              newRegistrationId = await _Hub.CreateRegistrationIdAsync();
         }
    
          var deviceRegistration = new DeviceRegistration
          {
              RegistrationId = newRegistrationId
          };
    
          return deviceRegistration;
      }
    
      public async Task<DeviceRegistration> UpdateRegistration(DeviceRegistration deviceUpdate, string registrationId, string[] tags)
      {
          RegistrationDescription registration;
          switch (deviceUpdate.Platform)
          {
              case "mpns":
                  registration = new MpnsRegistrationDescription(deviceUpdate.Handle);
                  break;
              case "wns":
                  registration = new WindowsRegistrationDescription(deviceUpdate.Handle);
                  break;
              case "apns":
                  registration = new AppleRegistrationDescription(deviceUpdate.Handle);
                  break;
              case "gcm":
                  registration = new FcmRegistrationDescription(deviceUpdate.Handle);
                  break;
              default:
                  throw new Exception("Bad Request");
          }
    
          registration.RegistrationId = registrationId;
          registration.Tags = new HashSet<string>(tags);
    
          await _Hub.CreateOrUpdateRegistrationAsync(registration);
    
          return deviceUpdate;
      }
    
      private static string ToHexString(string str)
      {
          var sb = new StringBuilder();
    
          var bytes = Encoding.Unicode.GetBytes(str);
          foreach (var t in bytes)
          {
              sb.Append(t.ToString("X2"));
          }
    
          return sb.ToString();
      }
    

Mobile App (Ionic 3 Cordova):
update phonegap-plugin-push to latest version: 2.3.0.

Mobile App (Ionic 4 Capacitor)
We didn't have to do anything it worked after the backend API was updated.

Hope this helps!

Hey,
I have device token like this, this is the one I am getting on my backend(.Net core service), when I use call Register()->ToHexString() function and eventually register with Azure Notification Hub, it does NOT throw any error But when I send the push notification via user Tags to that device, APNS says that "Pns handle is not valid" and push fails to go over. Have you come across this?

string Token ="{length=32,bytes=0xc125ed785e0a827d5957983ffa87436d...bc8fe979b25e8a6b}"

Regards
Sam,

from azure-notificationhubs-dotnet.

AntRemo avatar AntRemo commented on June 2, 2024

@tanujnarula I am using this approach

byte[] result = new byte[deviceToken.Length];
Marshal.Copy(deviceToken.Bytes, result, 0, (int) deviceToken.Length);
var token = BitConverter.ToString(result).Replace("-", "");

https://stackoverflow.com/a/58028222/3260560

from azure-notificationhubs-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.