The method below shows how I create a notification. I append a new item to the UserInfo
dictionary.
private UNNotificationRequest CreateNotification(Geofence geofence)
{
var content = new UNMutableNotificationContent();
content.Title = "title";
content.Subtitle = "subtitle";
content.Body = "This is the message body of the notification.";
content.Badge = 2;
content.UserInfo.Append(new KeyValuePair<NSObject, NSObject>((NSString)"id", (NSString)"bla"));
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
var request = UNNotificationRequest.FromIdentifier("test1", content, trigger);
return request;
}
The problem is as follows:
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
foreach (var item in response.Notification.Request.Content.UserInfo)
{
}
// Inform caller it has been handled
completionHandler();
}
The UserInfo
dictionary is always empty. Why is that and how can I fix this?
You need to get the logic in the switch statement
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
// Take action based on Action ID
switch (response.ActionIdentifier)
{
case "reply":
// Do something
break;
default:
// Take action based on identifier
if (response.IsDefaultAction)
{
// Handle default action...
var item = response.Notification.Request.Content.UserInfo;
}
else if (response.IsDismissAction)
{
// Handle dismiss action
}
break;
}
// Inform caller it has been handled
completionHandler();
}
It seems that you add UserInfo in wrong way . Check the following code
content.UserInfo = NSDictionary.FromObjectAndKey(new KeyValuePair<NSObject, NSObject>((NSString)"id", (NSString)"bla"));