0

I am trying to get DeviceToken of device in Xamarin iOS through RegisteredForRemoteNotifications and save it to Preferences, through API I save DeviceToken to database Server.

AppDelegate.cs

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    byte[] bytes = deviceToken.ToArray<byte>();
    string[] hexArray = bytes.Select(b => b.ToString("x2")).ToArray();
    DeviceToken = string.Join(string.Empty, hexArray);

    Preferences.Set("TokenDevice", DeviceToken);
}

I have saved the DeviceToken to the database, the DeviceToken of the device I get:

6b60feecad920471ccde5a3447ab22d3f820abae821daeac726cc7e6d0863465

I wrote an API to send notifications to that device:

[HttpPost]
public void SendNotification(string devicetoken, string title, string body, string link, string icon)
{
    try
    {
        dynamic data = new
        {
            to = devicetoken, // Uncoment this if you want to test for single device
                              // registration_ids = singlebatch, // this is for multiple user 
            priority = "high",
            notification = new
            {
                title = title,     // Notification title
                body = body,    // Notification body data
                link = link,       // When click on notification user redirect to this link
                icon = icon
            }
        };

        //var serializer = new JavaScriptSerializer();
        var json = JsonConvert.SerializeObject(data);
        Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(json);

        string SERVER_API_KEY = "AAAAqOId6Is:APA91bHJ5pQgLlanU8gwQwnpxdBlKS00i1xxxxxxxxxxxxxxxxxxxxxxxxxxx";
        string SENDER_ID = "72xxxxxxxx";

        WebRequest tRequest;
        tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = "application/json";
        tRequest.Headers.Add(string.Format("Authorization: key={0}", SERVER_API_KEY));

        tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

        tRequest.ContentLength = byteArray.Length;
        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse tResponse = tRequest.GetResponse();

        dataStream = tResponse.GetResponseStream();

        StreamReader tReader = new StreamReader(dataStream);

        String sResponseFromServer = tReader.ReadToEnd();

        tReader.Close();
        dataStream.Close();
        tResponse.Close();
    }
    catch (Exception)
    {
        throw;
    }
}

Where: SERVER_API_KEY and SENDER_ID I got from in console.firebase.google.com/Project settings/Cloud Messaging

enter image description here

I tried to test the API that sends notifications to the device I get the error: {\"multicast_id\":7343900550378569449,\"success\":0,\"failure\":1,\"canonical_ids\":0, \"results\":[{\"error\":\"InvalidRegistration\"}]}

enter image description here

I referenced this article. However I want to use this code because it allows me to set the Notification link and icon. Is this an outdated notification code? I searched the forums, and didn't get any reasonable results. Looking forward to everyone's help.

Chim Di Tru
  • 495
  • 4
  • 17
  • Suggest it's worth checking the config in Firebase server side for your APNS cert. When you download a cert for APNS you need to specify the bundle name and whether you're going to use sandbox or production APNS. Make sure Firebase has the same settings. – GregHNZ Jun 28 '22 at 07:11
  • Yes, I have done that. I created the Keys on developer.apple.com, I downloaded the .p8 file and uploaded it to firebase (Project settings/Cloud Messaging/Apple app configuration/APNs Authentication Key). I have tried sending the notification by sending test in Firebase Cloud Messaging and my app received the notification. However when I submit through the API as in my description above I get the error: InvalidRegistration. Do you have any other suggestions? – Chim Di Tru Jun 29 '22 at 02:02
  • The only other thing I can think of is to uninstall your app off the device, then install and run it again, and make sure you get the new token. Your token looks just like the ones we get, and that response is what we get from FCM when an _android_ device's token is no longer valid. We use AWS SNS for iOS, so I'm running out of similarities. Can you message that token via the FCM console? (I think you can do that with Android ones). – GregHNZ Jun 29 '22 at 05:36
  • On FCM I submit tests for all apps (This I get notifications on iOS devices). I sent a test via DeviceToken: `6b60feecad920471ccde5a3447ab22d3f820abae821daeac726cc7e6d0863465` (DeviceToken iOS). However I am not receiving notifications on the Device. I am confused in that: DeviceToken on iOS and DeviceToken on Android I see its structure different? Is the way I get the DeviceToken on iOS the wrong way? – Chim Di Tru Jun 29 '22 at 06:30
  • Android tokens and iOS tokens *are* different. Yours looks correct. I use a mechanism like this answer: https://stackoverflow.com/a/59304817/1254208 - I suspect you use one of the other answers on the same question. – GregHNZ Jun 29 '22 at 21:34

0 Answers0