2014-12-29 89 views
0

我正在創建一個使用Parse雲的應用程序。我正在嘗試將用戶的消息發送給所有其他人。在文本框寫消息,並按下發送後,委託調用下面的方法,併爲下圖所示的推入處理:向查詢中的所有用戶發送推送通知

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
//hide keyboard 
[textField resignFirstResponder]; 

NSString *message = self.broadcastText.text; 

//create a user query 
PFQuery *query = [PFUser query]; 
PFUser *current = [PFUser currentUser]; 
NSString *currentUsername = current[@"username"]; 
[query whereKey:@"username" notEqualTo:currentUsername]; 

//send push in background 
PFPush *push = [[PFPush alloc] init]; 
[push setQuery:query]; 
[push setMessage:message]; 
[push sendPushInBackground]; 

//clear text field 
textField.text = @""; 

return YES;} 

發生了什麼事是,當我發送郵件,發送者(在這種情況下我)也在接收推送通知。我試圖做的是獲取當前用戶的用戶名,然後創建一個用戶查詢,查詢用戶名不等於當前用戶用戶名的所有用戶。

但是,它沒有工作,該消息也被髮送給包括髮件人在內的所有用戶。注:我也嘗試使用[查詢whereKey:@「username」notEqualTo:currentUsername];僅用於調試,當我嘗試發送消息時,發件人和任何其他設備都不會收到它。 (實際上除了發件人以外沒有人應該收到)。

任何幫助將不勝感激。謝謝。

回答

1

你的問題是PFPush不能接受任何查詢,它需要一個PFInstallation查詢。當您存儲PFInstallation爲每個用戶,您可以添加指向當前用戶的字段,是這樣的:

PFInstallation *currentInstallation = [PFInstallation currentInstallation]; 
currentInstallation[@"user"] = [PFUser currentUser]; 
[currentInstallation saveInBackground]; 

然後執行安裝這樣的查詢:

PFQuery *installationQuery = [PFInstallation query]; 
PFUser *current = [PFUser currentUser]; 
[installationQuery whereKey:@"user" notEqualTo:current]; 

然後,繼續與您的推動使用此查詢:

PFPush *push = [[PFPush alloc] init]; 
[push setQuery:installationQuery]; // <<< Notice query change here 
[push setMessage:message]; 
[push sendPushInBackground]; 
+0

偉大的解決了我的問題謝謝:) @Logan – RB12

0

理論上,您發送推送通知給所有訂閱了一些渠道好的用戶。 現在你有一個包含所有用戶和所有頻道的表格。有些用戶訂閱其他的不是。首先爲安裝創建一個查詢,然後查找不是當前用戶的用戶。

PFQuery *pushQuery = [PFInstallation query]; 
[pushQuery whereKey:"user" notEqualTo:[PFUser currentUser]]; 

創建一個Push對象並使用此查詢。

PFPush *push = [[PFPush alloc] init]; 
[push setQuery:pushQuery]; // Set our Installation query 
[push setMessage:@"Ciao."]; 
[push sendPushInBackground]; 

在pushQuery您可以使用其他鍵,例如:設備ID,installationID,設備類型等。 我用解析雲而是讓你需要嘗試這個代碼,我從來沒有使用推送通知。