在 Objective-C 中解析推送通知

Parse Push Notification in Objective-C

我正在尝试使用 Parse.com SDK 实现 iOS 推送通知。 问题是,有时没有任何错误日志就不会发送推送通知。我已将代码更改为:

- (IBAction)send:(id)sender {
    PFQuery *usernameQuery = [PFUser query];
    [usernameQuery whereKey:@"objectId" containedIn:self.recipients];

    PFQuery *pushQuery = [PFInstallation query];
    [pushQuery whereKey:@"user" matchesQuery:usernameQuery];

    PFPush *push =[[PFPush alloc] init];
    [push setQuery:pushQuery];
    [push setMessage:[NSString stringWithFormat:@"%@: %@", [PFUser currentUser].username, self.kwik]];
    [push sendPushInBackground];

    [self.navigationController popToRootViewControllerAnimated:YES];

    NSLog(@"%p", push);
}

我不知道每次用户发送推送时分配的 PFPush 是否仍然会出现此问题。 在我将代码更改为这个代码之前,我在 viewDidLoad 方法中调用 self.push = [[PFPush alloc] init];,因为我不喜欢每次用户因为内存使用而发送推送通知时分配一个新的 PFPush。 我现在的问题是:每次用户发送推送时分配一个新的 PFPush 对象是否重要,或者我可以在 viewDidLoad 方法中分配它吗?

行:

PFPush *push =[[PFPush alloc] init];

创建一个对象,但是对它的引用是短暂的(完成异步推送请求所花费的时间),并且在ARC项目中,代码将在编译时插入以释放它,所以没有需要担心内存分配问题。

您在获取推送时可能遇到了无关的问题。一种调试方法是使用 block variety of push in background called sendPushInBackgroundWithBlock。有了它,您可以检查是否有错误。