【问题标题】:Sending Push Notification within certain Miles using Parse使用 Parse 在特定里程内发送推送通知
【发布时间】:2014-09-01 12:36:03
【问题描述】:

我使用Parse 发送和接收推送通知。我已经仔细阅读了教程并成功实现了它。 现在我需要在用户选择的特定里程内向用户发送通知。

例如:如果用户选择100,那么通知应该发送给所有安装了我的应用程序并且距离用户(正在广播通知的用户)100英里以内的用户当前位置。

我在 Parse 的网站上找到了相同的代码 sn-p,但如果我使用以下代码中注释的第二个查询,我没有收到任何通知 如果我使用第二个查询,则不会调用方法 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];

【问题讨论】:

  • 'location' 在用户表中是一个地理点正确吗?而且您在安装表中有一个“用户”列?
  • 在 Location 列中,您输入的纬度或经度共享该值
  • @Jacob :恐怕我的后端没有表。它只是我看到并按原样实现的查询。
  • @Maul : 很抱歉我从来不知道餐桌上的东西...
  • 您使用解析权限而不是注册过程意味着它会在解析中创建用户表。如果您想构建通知过程,则必须进入解析设置并检查启用客户端推送是否已启用,并且您必须上传启用推送的证书

标签: ios ios7 push-notification parse-platform


【解决方案1】:

我在我的一个项目中做过同样的事情,希望下面的代码和步骤对你有帮助。

首先在你的Appdelgate's方法didRegisterForRemoteNotificationsWithDeviceToken放下面的代码sn -p

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

然后将以下代码 sn-p 从您要发送通知的位置发送给其他人

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

【讨论】:

  • 完美运行!!非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 2015-01-06
相关资源
最近更新 更多