从设备到设备的Firebase谷歌云消息



我不明白如何将消息从iOS设备发送到另一个iOS设备,并试图理解Firebase通知和谷歌云消息。

Firebase通知例如,从服务器可以向设备发送消息。

谷歌云消息:它从服务器向设备(下游)或设备向服务器(上游)发送消息!!

上游示例:

[[FIRMessaging message]sendMessage:(nonnull NSDictionary *)message
to:(nonnull NSString *)receiver
withMessageID:(nonnull NSString *)messageID
timeToLive:(int64_t)ttl;

如果我需要在设备之间发送推送消息,该怎么办!这是否意味着在设备向服务器发送消息后,我必须编程firebase服务器向客户端发送推送?真是令人困惑!

不,您不能在iOS上使用firebase执行此操作,您应该调用firebase上的服务,该服务将向其他设备发送通知。APNS和GCM在服务器设置方面有点不同。

对于GCM,您只需要将API密钥添加到对https://android.googleapis.com/gcm/send这可以在任何地方完成服务器、移动设备等。您所需要的只是目标设备设备令牌和API密钥。

APNS的工作方式不同——你需要附加你在苹果开发者门户网站上创建的服务器SSL证书,以对自己进行身份验证并向设备发送推送通知。我不知道你如何在iOS设备上实现这一点。

这个线程阐明了GCM和Firebase,之间的真正区别

Firebase 实时推送通知

https://firebase.google.com/support/faq/#gcm-不是

Firebase和GCM不同,但它们可以用于实现相同的目标。希望它能帮助你。

我认为Firebase目前没有能力处理这种情况。你需要一些服务器端的代码来处理它

[[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是一家专门的推送通知公司,而且免费的基本计划似乎足够好,可以处理你需要的东西,silimar了解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

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

应用程序中的委派。然后,当你希望你的用户设备1向另一个设备2发送推送时,假设你已经以某种方式将设备1的自定义id发送到设备2,你可以使用该自定义id将推送通知有效负载发送到Batch.com的API,Batch将处理服务器端的内容到Apple APN,你的推送通知将显示在设备2上。

最新更新