gpt4 book ai didi

ios - 使用 Parse 在一定英里内发送推送通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:27:43 27 4
gpt4 key购买 nike

我用过Parse用于发送和接收推送通知。我仔细地遵循了教程并成功地实现了它。现在我需要在用户选择的一定里程内向用户发送通知。

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

我在 Parse 的网站上找到了一个代码片段,但如果我使用在以下代码中注释的第二个查询,我不会收到任何通知如果我使用第二个查询,则不会调用方法 didReceiveRemoteNotification.只有当我使用第一个查询时,我才能成功收到通知。

我对推送通知和 Parse 比较陌生。让我知道我是否遗漏了一些愚蠢的东西,以便我可以解决它。我已经围绕这个问题很长时间了,所以请大家帮忙。

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

最佳答案

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

首先在您的Appdelgate 的 方法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;
}
}];

然后将以下代码片段放在您要向其他人发送通知的位置

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

关于ios - 使用 Parse 在一定英里内发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25606030/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com