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:
Open
Entitlements.plist
and ensure thatEnable Push Notifications
is checked when viewed in theEntitlements
tab. Then, ensure the APS Environment setting is set to development when viewed in the Source tab.Make sure that you are testing the remote-notification in a real device instead of a simulator. A simulator does not support remote-notification.
Make sure that you agreed receiving notification permission.
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
Why Does My C# Array Lose Type Sign Information When Cast to Object
How to Call a Function of a C++ Dll That Accepts a Parameter of Type Stringstream from C#
Logoff Interactive Users in Windows from a Service
How to Delete Cookies on an ASP.NET Website
Why Accessviolationexception Cannot Be Caught by .Net4.0
How to Consume a Blazor Component as a Web Component Within a Regular Non-Blazor HTML Page
Insert into SQL Db a String That Contain Special Character '
C# Invalid Attempt to Call Read When Reader Is Closed
Extract Content from Div Tag C# Regex
How Could the New Async Feature in C# 5.0 Be Implemented with Call/Cc
Can You Call a C# Dll from a C Dll
Download a File from Azure Devops Server Writes Wrong Data to The File
Xamarin Android Alarm Manager Issue