2014-09-01 68 views
2

我使用Parse來發送和接收推送通知。我已經仔細地跟隨了教程併成功實施了它。 現在我需要在用戶選擇的一定範圍內向用戶發送通知。使用Parse在特定里程內發送推送通知

例如:如果用戶選擇,則應該將通知發送給已安裝我的應用並且距離用戶(廣播通知的用戶)當前位置100英里內的所有用戶。

我發現對於同在解析網站的代碼片段,但我沒有收到任何通知,如果我使用的是下面的代碼 方法didReceiveRemoteNotification評論說,如果我使用第二種查詢 不叫第二個查詢。 只有當我使用第一個查詢時,我才能成功接收通知。

我更新的推送通知和解析。讓我知道,如果我錯過了一些愚蠢的東西,以便我可以修復它。我一直圍繞這個問題很長一段時間,所以,請大家幫忙。

PFGeoPoint *myLoc = [PFGeoPoint geoPointWithLatitude:myLatitude longitude:myLongitude]; 

     PFQuery *userQuery = [PFUser query]; 
     [userQuery whereKey:@"location" 
       nearGeoPoint:myLoc 
       withinMiles:100]; 

     PFQuery *myQuery = [PFInstallation query]; 
     [myQuery whereKey:@"deviceType" equalTo:@"ios"]; 
     //[myQuery whereKey:@"user" matchesQuery:userQuery]; 


     NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys: 
           txt.text, @"alert", 
           @"1", @"badge", 
           @"sound.caf", @"sound", 
           nil]; 


     PFPush *push = [[PFPush alloc] init]; 
     [push setQuery:myQuery]; 
     [push setData:data]; 
     [push sendPushInBackground]; 
+0

「位置」是地理點正確?你在安裝表中有'用戶'欄? – Jacob 2014-09-01 14:18:10

+0

在地點欄你把緯度或經度分享的價值 – Maul 2014-09-02 05:56:46

+0

@Jacob:我恐怕我沒有在我的後端表。它只是我看到並實施它的查詢。 – iCodeAtApple 2014-09-02 06:06:23

回答

5

我在我的項目中做了同樣的事情,希望以下代碼和步驟對您有所幫助。

首先在你Appdelgate's方法didRegisterForRemoteNotificationsWithDeviceToken放下面的代碼片段

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) { 
     if (!error) { 
      NSLog(@"User is currently at %f, %f", geoPoint.latitude, geoPoint.longitude); 
      PFInstallation *currentInstallation = [PFInstallation currentInstallation]; 
      [currentInstallation setDeviceTokenFromData:deviceToken]; 
      currentInstallation[@"location"] = geoPoint; 
      [currentInstallation saveInBackground]; 
     } else { 
      NSLog(@"%@", error); 
      return; 
     } 
    }]; 

並把下面的代碼片段從要在user表發送通知給其他人

PFInstallation *installation = [PFInstallation currentInstallation]; 
    NSLog(@"%@",installation); 
    [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) 
    { 
     if (!error) 
     { 
      PFQuery *userQuery = [PFInstallation query]; 
      [userQuery whereKey:@"location" 
        nearGeoPoint:geoPoint 
        withinMiles:100.00]; 
      NSString *str_CurrentDeviceToken = [NSString stringWithFormat:@"%@",[installation objectForKey:@"deviceToken"]]; 
      [userQuery whereKey:@"deviceToken" notEqualTo:str_CurrentDeviceToken]; 
      NSLog(@"%@",userQuery); 


      NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys: 
            @"Hello", @"alert", 
            @"1", @"badge", 
            @"", @"sound", 
            nil]; 


      PFPush *push = [[PFPush alloc] init]; 
      [push setQuery:userQuery]; 
      [push setData:data]; 
      [push sendPushInBackground]; 
     } 
     else 
     { 
      NSLog(@"%@", error); 
      return; 
     } 
    }]; 
+0

完美工作!非常感謝你:) – iCodeAtApple 2014-09-02 10:53:26

+0

歡迎你!!!!! – Maul 2014-09-02 10:56:59