PFPush handlePush崩溃:NSCFDictionary长度

时间:2015-05-25 17:34:04

标签: ios parse-platform

当应用程序打开并且收到推送通知时,我发生了崩溃。

[PFPush handlePush:userInfo];

JSON是一个简单的警报:标题,正文和一个十六进制数字的自定义字段。

USERINFO:

{
    aps =     {
        alert =         {
            body = "Test Body";
            title = "Test Title";
        };
    };
    url = "miner://item/5528c5aeaacfce1fd2d527dd";
}

2 个答案:

答案 0 :(得分:1)

刚检查了您的示例代码并且您是对的:这种字符串消息在PFPush handlePush处理时崩溃:

发生这种情况的原因: Parse同时支持iOS和Android PushNotification服务,这意味着它不能在通用JSON字符串中使用任何特定于服务的格式。

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"
        }

在Parse中,您需要使用不同的格式类型,如

{
  "alert": "Tune in for the World Series, tonight at 8pm EDT",
  "sound": "chime",
  "title": "Baseball News"
}

在iOS应用中,这将是上面JSON字符串中的userInfo对象:

{
    aps =     {
        alert = "Tune in for the World Series, tonight at 8pm EDT";
        sound = chime;
    };
    title = "Baseball News";
}

在此userInfo中,警报是NSString而不是NSDictionary。当Parse SDK尝试处理它时,它会向实例发送一条长度消息 - 这会导致崩溃。

来源: https://www.parse.com/questions/json-format-to-send-notification-from-parse https://parse.com/questions/json-push-notification-format-for-web-console-for-android-and-ios

更多示例: https://parse.com/docs/rest/guide/#push-notifications-sending-pushes

Cheerz, 亚当

答案 1 :(得分:0)

作为解决方法,我编写了以下代码来确定alert对象是否是字典,如果是,请使用userInfo重新构建body字典。 alert字典作为简单的字符串警报。

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];
        }
    }
}