對於iOS 9和更早版本,我創建了一些通知數據以及一些用戶數據的字典,最終在創建本地通知時將該數據傳遞給UserInfo
的UserInfo
屬性。 在ReceivedLocalNotification
覆蓋,我可以處理收到的通知之前,我檢查使用notification.UserInfo.ContainsKey()
來確定它是哪種類型的通知,然後適當地處理它。c#iOS 10通知 - GetDictionaryOfValuesFromKeys()錯誤
爲了迎合新的iOS 10更改,我通過將值傳遞給UNMutableNotificationContent
的UserInfo
屬性來保存附加數據。當我嘗試在iOS 10中檢索這些額外數據時,會發生挑戰。
我實現了UNUserNotificationCenterDelegate
類並覆蓋DidReceiveNotificationResponse
函數。在這個函數裏面,我嘗試使用GetDictionaryOfValuesFromKeys()
函數來訪問額外的數據,但是每次碰到這行時都會崩潰。
下面是我的代碼:
我傳遞給通知
NSMutableDictionary userInfo = new NSMutableDictionary();
userInfo.Add((NSString)Constants.PushNotificationInfoKeys.NOTIFICATION_TYPE, (NSNumber)((int)Constants.PushNotificationTypes.DRUG_ALERT));
userInfo.Add((NSString)Constants.PushNotificationInfoKeys.NOTIFICATION_IDENTIFIER, (NSString)alert.Id);
//Pass this data to the function that creates a local notification
alarmService.CreateAlarm(alarmTime, string.Format(..., userInfo);
的創建報警功能
public Result<UILocalNotification> CreateAlarm(DateTime alertTime, string alertBody, string alertAction, NSMutableDictionary userInfo)
{
try
{
//Code truncated for clarity
// create the notification
var notification = new UILocalNotification();
//Set the metadata for the alert
notification.UserInfo = userInfo;
// schedule it
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
return Result<UILocalNotification>.Success(notification);
}
catch (Exception ex)
{
return Result<UILocalNotification>.Failure(ex.Message);
}
}
額外的數據我怎麼在檢索數據ReceivedLocalNotification
if (notification.UserInfo.ContainsKey(new NSString(Constants.PushNotificationInfoKeys.IS_RESPONSE_TO_SNOOZE)))
{
// Do something here
}
我如何在iOS中10檢索數據:
public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
try
{
NSString[] keys = { new NSString(Constants.PushNotificationInfoKeys.NOTIFICATION_TYPE) };
var mystring = new NSString(Constants.PushNotificationInfoKeys.NOTIFICATION_TYPE);
var alertId = response.Notification.GetDictionaryOfValuesFromKeys(keys).ValueForKey(mystring); //App crashes on this line
}catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
}
}
我如何可以訪問UserInfo
財產iOS的10?有任何想法嗎? 謝謝。