PFPush handlePush崩溃:NSCFDictionary长度
当应用程序打开并收到推送通知时,我收到崩溃。
[PFPush handlePush:userInfo];
JSON是一个简单的警报:标题,正文和一个hex数字的自定义字段。
用户信息:
{ aps = { alert = { body = "Test Body"; title = "Test Title"; }; }; url = "miner://item/5528c5aeaacfce1fd2d527dd"; }
刚刚检查了你的示例代码,你是正确的:这种string消息是由PFPush handlePush处理时崩溃:
发生这种情况的原因是:Parse同时支持iOS和Android PushNotification服务,这意味着它不能在通用的JSONstring中使用任何特定于服务的格式。
iOS使用这些格式:
{ "aps" : { "alert" : { "title" : "Game Request", "body" : "Bob wants to play poker", "action-loc-key" : "PLAY" }, "badge" : 5, }, "acme1" : "bar", "acme2" : [ "bang", "whiz" ] }
和Android
"data": { "title": "Push Title", "message": "Push message for Android", "customData": "Custom data for Android" }
在parsing中,您需要使用不同的格式types
{ "alert": "Tune in for the World Series, tonight at 8pm EDT", "sound": "chime", "title": "Baseball News" }
在iOS应用程序中,这将是上面的JSONstring中的userInfo对象:
{ aps = { alert = "Tune in for the World Series, tonight at 8pm EDT"; sound = chime; }; title = "Baseball News"; }
在这个userInfo中,警报是一个NSString而不是一个NSDictionary。 当parsingSDK尝试处理它时,它会向实例发送一个长度消息 – 这会导致崩溃。
来源: https : //www.parse.com/questions/json-format-to-send-notification-from-parse https://parse.com/questions/json-push-notification-format-for-web-console -用于-Android的和IOS
更多示例: https : //parse.com/docs/rest/guide/#push-notifications-sending-pushes
Cheerz,Adam
作为解决方法,我编写了以下代码以确定alert
对象是否是字典,如果是, userInfo
使用alert
字典的body
重新构builduserInfo
字典,作为简单的string警报。
NSDictionary *apsDict=userInfo[@"aps"]; if (apsDict != nil) { id alert=apsDict[@"alert"]; NSMutableDictionary *mutableUserInfo =(NSMutableDictionary *)userInfo; if (alert !=nil) { if ([alert isKindOfClass:[NSDictionary class]]) { mutableUserInfo=[userInfo mutableCopy]; NSMutableDictionary *mutableApsDict=[apsDict mutableCopy]; mutableUserInfo[@"aps"]=mutableApsDict; mutableApsDict[@"alert"]=alert[@"body"]; } [PFPush handlePush:mutableUserInfo]; } } }