2017-02-14 59 views
0

对于iOS 9和更早版本,我创建了一些通知数据以及一些用户数据的字典,最终在创建本地通知时将该数据传递给UserInfoUserInfo属性。 在ReceivedLocalNotification覆盖,我可以处理收到的通知之前,我检查使用notification.UserInfo.ContainsKey()来确定它是哪种类型的通知,然后适当地处理它。c#iOS 10通知 - GetDictionaryOfValuesFromKeys()错误

为了迎合新的iOS 10更改,我通过将值传递给UNMutableNotificationContentUserInfo属性来保存附加数据。当我尝试在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?有任何想法吗? 谢谢。

回答

0

一个小小的研究之后,我才知道,你可以从响应对象访问DidReceiveNotificationResponse功能像这里面的用户信息属性:

var userInfo = response.Notification.Request.Content.UserInfo; 

      if (userInfo != null) 
      { 
       var value = userInfo.ValueForKey(mystring); 

       if (userInfo.ContainsKey(new NSString(Constants.PushNotificationInfoKeys. NOTIFICATION_TYPE))) 
       { 
        //Do something here 
       } 
       Console.WriteLine("Value :" + value); 

      }