我认为 Firebase 目前还没有能力处理这种情况。您需要一些服务器端代码来处理它。要么你可以得到托管并像一个 php 端点一样可以用来合并
[[FIRMessaging message]sendMessage:(nonnull NSDictionary *)message
to:(nonnull NSString *)receiver
withMessageID:(nonnull NSString *)messageID
timeToLive:(int64_t)ttl;
编码并使其工作,或者您需要找到另一个可以用作后端的服务。
https://techcrunch.com/2016/02/02/batch-now-integrates-with-firebase-to-create-a-parse-alternative/
这家 Batch.com 公司似乎是我迄今为止找到的最佳解决方案。我已经能够让用户设备将 json 有效负载发送到其服务器上的端点,然后将自定义推送通知发送到特定的目标设备。看起来 Batch 是专门的 Push Notification 公司,而且免费的基本计划似乎足以满足您的需求,这与 Parse 的工作方式类似。
这是我为发送推送通知 Objective C 编写的实际代码。(还有一个 Swift 2 和 Swift 3 示例,您可以从 Batch.com 下载)
NSURL *theURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.batch.com/1.1/(your API code here)/transactional/send"]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0f];
[theRequest setValue:@"(your other API key here" forHTTPHeaderField:@"X-Authorization"];
NSDictionary *messageDict = [[NSDictionary alloc]initWithObjectsAndKeys:@"Hey This is a Push!", @"title", @"But it's a friendly push. Like the kind of push friends do to each other.",@"body", nil];
NSArray *recipientsArray = [[NSArray alloc]initWithArray:someMutableArrayThatHasUsersFirebaseTokens];
NSDictionary *recipientsDict = [[NSDictionary alloc]initWithObjectsAndKeys:recipientsArray, @"custom_ids", nil];
NSDictionary *gistDict = @{@"group_id":@"just some name you make up for this pushes category that doesn't matter",@"recipients":recipientsDict,@"message":messageDict, @"sandbox":@YES};
NSError *jsonError;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:gistDict options:NSJSONWritingPrettyPrinted error:&jsonError];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:jsonData];
NSOperationQueue *queue1 = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:theRequest queue:queue1 completionHandler:^(NSURLResponse *response, NSData *POSTReply, NSError *error){
if ([POSTReply length] >0 && error == nil){
dispatch_async(dispatch_get_main_queue(), ^{
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding:NSASCIIStringEncoding];
NSLog(@"BATCH PUSH FINISHED:::%@", theReply);
});
}else {
NSLog(@"BATCH PUSH ERROR!!!:::%@", error);
}
}];
使用 Cocoa Pods 安装 Batch 非常容易。
我也使用此代码使其工作:
在应用委托中:
@import Batch
在 didFinishLaunching 中:
[Batch startWithAPIKey:@"(your api key)"]; // dev
[BatchPush registerForRemoteNotifications];
然后在应用委托中:
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"InstanceID token: %@", refreshedToken);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:refreshedToken forKey:@"pushKey"];
[defaults synchronize];
BatchUserDataEditor *editor = [BatchUser editor];
[editor setIdentifier:refreshedToken]; // Set to `nil` if you want to remove the identifier.
[editor save];
[self connectToFcm];
}
除了在 Batch.com 网站上解释的设置和安装内容之外,您就是这样做的。
一旦你从 firebase 获得你的令牌,你基本上就用
在 Batch 上注册它
BatchUserDataEditor *editor = [BatchUser editor];
[editor setIdentifier:refreshedToken];
[editor save];
在应用代理中。然后,当您希望您的用户 device1 向另一个 device2 发送推送时,假设您已经以某种方式将 device1 的自定义 id 发送到 device2,您可以使用该自定义 id 将推送通知有效负载发送到 Batch.com 的 API,Batch 将处理服务器Apple APN 附带的内容,您的推送通知出现在 device2 上。