How to Get Device Token in iOS 13 with Xamarin

How to get device token in iOS 13 with Xamarin?

Looks like I found the answer myself:

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

Using this code I was able to get a device token and send a notification.

How to receive the ASPN Token in Xamarin Forms iOS-App

Some points to check if RegisteredForRemoteNotification not called:

  1. Open Entitlements.plist and ensure that Enable Push Notifications is checked when viewed in the Entitlements tab. Then, ensure the APS Environment setting is set to development when viewed in the Source tab.

  2. Make sure that you are testing the remote-notification in a real device instead of a simulator. A simulator does not support remote-notification.

  3. Make sure that you agreed receiving notification permission.

  4. Make sure the certification you use has enabled the push notification ability.

Refer: configuring-the-remote-notifications-environment

deviceToken of IOS Device

In your FinishedLaunching method, register the app for remote notifications, through the UIApplication object you get in it:

// Pass the UIRemoteNotificationType combinations you want
app.RegisterForRemoteNotificationTypes(UIRemoteNotificationType.Alert |
UIRemoteNotificationType.Sound);

Then, in your AppDelegate class, override the RegisteredForRemoteNotifications method:

public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
{
// The device token
byte[] token = deviceToken.ToArray();
}

You also have to override the FailedToRegisterForRemoteNotifications method, to handle the error, if any:

public override void FailedToRegisterForRemoteNotifications (UIApplication application, NSError error)
{
// Do something with the error
}

Does iOS 13 has new way of getting device notification token?

The way you do it is fine and it should continue to work on iOS 13. But some developers do it like this. To convert Data into base-16 strings, they call description, which returns something like

<124686a5 556a72ca d808f572 00c323b9 3eff9285 92445590 3225757d b83997ba>

And then they trim < and > and remove spaces.

On iOS 13 the description called on token data returns something like

{ length = 32, bytes = 0xd3d997af 967d1f43 b405374a 13394d2f ... 28f10282 14af515f }

Which obviously makes this way broken.

Another example of wrong implementation (already edited to include correct implementation as well).

Some more examples might be found in this thread.

Xamarin iOS Firebase Cloud Messaging - Push Notifications - can not send test message with device token

I think that you should register token with this method

[Export("messaging:didReceiveRegistrationToken:")]
public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
{
// Monitor token generation: To be notified whenever the token is updated.
Console.WriteLine("Received token: " + fcmToken);
// Handle here how your app is storing token locally or send it to server
// Note: This callback is fired at each app startup and whenever a new token is generated.
}

How to save Firebase Device Token in Xamarin.Forms Android

You can't use DependencyService to send data from platform project to shared project.

I would recommend you to use MessagingCenter to send the data:

MessagingCenter.Send<MainPage>(this, "Hi");

And in shared project:

MessagingCenter.Subscribe<MainPage> (this, "Hi", (sender) =>
{
// Do something whenever the "Hi" message is received
});

If you want to get/save some data in Xamarin.forms, you can use Xamarin.Essentials: Preferences
:

Save the value with key:

Preferences.Set("my_key", "my_value");

Get the value with key:

var myValue = Preferences.Get("my_key", "default_value");


Related Topics



Leave a reply



Submit