2015-10-16 168 views
0

我正在尝试向特定设备发送推送通知,但如果我发送到某个频道,我只能使其工作。解析iOS:向特定人员/设备发送推送通知

“userQuery.countObjects”正在返回1个用户发现,所以我不知道什么是错,帮助!

这里是我的代码:

   PFUser *user1= [friendUsers objectAtIndex:0]; 

       // Associate the device with a user 
       PFInstallation *installation = [PFInstallation currentInstallation]; 
       installation[@"user"] = [PFUser currentUser]; 
       [installation saveInBackground]; 

       PFQuery *userQuery = [PFUser query]; 
       [userQuery whereKey:@"fbId" equalTo:user1[@"fbId"]]; 
       userQuery.limit = 1; 

       NSLog(@"cnt=%d", userQuery.countObjects); 

      // Find devices associated with these users 
      PFQuery *pushQuery = [PFInstallation query]; 
      [pushQuery whereKey:@"user" matchesQuery:userQuery]; 

      // Send push notification to query 
      PFPush *push = [[PFPush alloc] init]; 
      [push setQuery:pushQuery]; // Set our Installation query 
      [push setData:data]; 
      [push sendPushInBackground]; 

解析推仪表板显示了我这个,我已删除了ID

PUSH ID 
dkfo3WhNod 

TARGETING 
user advanced operator ($inQuery {"className"=>"_User", "limit"=>"1", "where"=>{"fbId"=>"xxxx"}}) 
SENDING TIME 
October 16th, 2015 at 4:37 PM 

EXPIRATION 
None 

FULL TARGET 
{ 
"user": { 
    "$inQuery": { 
    "className": "_User", 
    "limit": "1", 
    "where": { 
    "fbId": "xxx" 
    } 
    } 
    } 
} 

FULL DATA 
{ 
    "alert": "Hi", 
    "badge": "Increment", 
    "sound": "hi.wav" 
} 
+0

http://stackoverflow.com/questions/27690215/sending-push-notification-to-all - 用户在查询 –

+0

@NicolasS良好的信息,但不是问题的根源。安装查询,在这种情况下称为'pushQuery',正在设置 – Russell

+0

奇怪的是,这段代码现在工作正常! – cal

回答

0

您在installation对象与关联是在正确的轨道上他们各自的用户进行定位,但我认为有些用户通过fbId查询出错了。

NSLog(@"cnt=%d", userQuery.countObjects);返回一个不是因为有一个用户找到(查询永远不会执行),而是因为它计数单个查询对象。

仔细检查用户查询是否正在返回您认为的内容。分别执行查询并打印结果。

另外,它看起来像你已经有用户数组friendUsers。由于您已经拥有指针,因此不需要单独的用户查询。

如果您想发送推送通知所有用户在阵列中,请尝试以下

// Find devices associated with these users 
PFQuery *pushQuery = [PFInstallation query]; 
[pushQuery whereKey:@"user" containedIn:friendUsers]; 

// Send push notification to query 
PFPush *push = [[PFPush alloc] init]; 
[push setQuery:pushQuery]; // Set our Installation query 
[push setData:data]; 
[push sendPushInBackground]; 

同样,如果你只关心第一个用户,只需使用whereKey:@"user" equalTo:[friendUsers objectAtIndex:0]

作为有用的提示,您还需要将尽可能多的逻辑卸载到云代码。而不是使用户关联的客户端,你可以在云代码beforeSave功能检查了这一点下面

// Make sure all installations point to the current user 
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) { 

    Parse.Cloud.useMasterKey(); 
    if (request.user) { 
     request.object.set('user', request.user); 
    } else { 
     request.object.unset('user'); 
    } 
    response.success(); 
}); 
+0

谢谢你,原来的代码现在工作正常! – cal

相关问题