我有一个Xamarin。接收推送通知的Forms应用程序。收到通知后,用户点击通知,应用程序会处理通知附带的数据,并采取相应行动。但我对应用程序的iOS部分有问题。它以json格式显示数据,而不是用户友好的通知文本,这不是用于此目的的。以下是通知的发送方式:
private static async Task SendTemplateNotificationsAsync(string json, ILogger log)
{
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(NotificationDispatcherConstants.FullAccessConnectionString, NotificationDispatcherConstants.NotificationHubName);
Dictionary<string, string> templateParameters = new Dictionary<string, string>();
foreach (var tag in NotificationDispatcherConstants.SubscriptionTags)
{
templateParameters["messageParam"] = json;
try
{
await hub.SendTemplateNotificationAsync(templateParameters, tag);
...
}
catch (Exception ex)
{
log.LogInformation($"Failed to send template notification: {ex.Message}");
}
}
}
以下是常数:
public static string APNTemplateBody { get; set; } = "{"aps":{"alert":"$(messageParam)"}}";
public const string NotificationTokenKey = "NotificationTokenKey";
以下是在AppDelegate.cs:中处理通知的方式
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
completionHandler();
NSDictionary userInfo = response.Notification.Request.Content.UserInfo;
ProcessNotification(userInfo);
}
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
NSDictionary userInfo = notification.Request.Content.UserInfo;
ProcessNotification(userInfo);
}
void ProcessNotification(NSDictionary options)
{
Task.Run(() =>
{
// make sure we have a payload
if (options != null && options.ContainsKey(new NSString("aps")))
{
// get the APS dictionary and extract message payload. Message JSON will be converted
// into a NSDictionary so more complex payloads may require more processing
NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
string payload = string.Empty;
NSString payloadKey = new NSString("alert");
if (aps.ContainsKey(payloadKey))
{
payload = aps[payloadKey].ToString();
}
if (!string.IsNullOrWhiteSpace(payload))
{
if (App.UserContext.IsEmployee)
{
App.NewCall(payload);
}
}
}
else
{
Debug.WriteLine($"Received request to process notification but there was no payload.");
}
});
}
应用程序。NewCall(有效载荷(;是使负载由应用程序处理的代码,这是正确的。但是我不希望这个有效负载显示为通知文本。如何将文本设置为不同的、用户友好的内容?
如果您只想显示默认的UI:,则不需要处理消息
请参阅本文档中的步骤12,确保有效载荷的格式正确:
void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
{
// Check to see if the dictionary has the aps key. This is the notification payload you would have sent
if (null != options && options.ContainsKey(new NSString("aps")))
{
//Get the aps dictionary
NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
string alert = string.Empty;
//Extract the alert text
// NOTE: If you're using the simple alert by just specifying
// " aps:{alert:"alert msg here"} ", this will work fine.
// But if you're using a complex alert with Localization keys, etc.,
// your "alert" object from the aps dictionary will be another NSDictionary.
// Basically the JSON gets dumped right into a NSDictionary,
// so keep that in mind.
if (aps.ContainsKey(new NSString("alert")))
alert = (aps [new NSString("alert")] as NSString).ToString();
//If this came from the ReceivedRemoteNotification while the app was running,
// we of course need to manually process things like the sound, badge, and alert.
if (!fromFinishedLaunching)
{
//Manually show an alert
if (!string.IsNullOrEmpty(alert))
{
var myAlert = UIAlertController.Create("Notification", alert, UIAlertControllerStyle.Alert);
myAlert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(myAlert, true, null);
}
}
}
}
如果你想使用自定义用户界面,你可以创建一个通知内容扩展并在那里创建你的UI。