提问者:小点点

从设备到设备的Firebase Google云消息传递


我不明白如何从iOS设备向另一个iOS设备发送消息,我试图理解Firebase通知和Google云消息之间的区别。

null

Google Cloud Messaging:它将消息从服务器发送到设备(下游)或设备发送到服务器(上游)!!

上游示例:

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

如果我需要从一个设备发送一个推送消息到另一个设备呢!这是否意味着在设备发送消息到服务器之后,我必须编程firebase服务器发送推送到客户端?真让人摸不着头脑!


共2个答案

匿名用户

不,你不能在iOS上使用firebase,你应该做的是调用firebase上的一个服务,它会向其他设备发送一个通知。APNS和GCM在服务器设置方面有点不同。

对于GCM,您只需要在对https://android.googleapis.com/GCM/send进行的POST调用中添加API密钥,这可以在任何地方服务器,移动设备等进行。您所需要的只是目标设备设备令牌和API密钥。

APNS的工作方式不同,您需要附加您在Apple developer portal上创建的服务器SSL证书来验证您自己,并向设备发送推送通知。我不知道如何在iOS设备上实现这一点。

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

Firebase实时推送通知

null

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

匿名用户

我认为Firebase目前还不能处理这种情况。你需要一些服务器端代码来处理它。或者,您可以获得宿主,并像php端点一样创建可用于合并

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

代码并使其工作,或者您需要找到另一个可以充当后端的服务。

null

这家Batch.com公司似乎是目前为止我找到的最好的解决方案。我已经能够让用户的设备将json有效载荷发送到他们服务器上的端点,然后该端点将定制的推送通知发送到特定的目标设备。看起来Batch是一个专门的推送通知公司,而且它看起来像是免费的基本计划足够好的处理你将需要的东西,silimer是如何Parse工作的。

下面是我为发送推送通知Objective C编写的实际代码(还有一个Swift2和Swift3示例,您可以从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);
    }
}];

批处理是相当容易安装与可可荚。

我还使用了以下代码来使其工作:

在应用程序委托中:

@import Batch

在DidFinishLaunching中:

[Batch startWithAPIKey:@"(your api key)"]; // dev
[BatchPush registerForRemoteNotifications];

然后在app Delegate后面:

- (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获得令牌后,基本上可以在批处理中注册它

BatchUserDataEditor *editor = [BatchUser editor];
[editor setIdentifier:refreshedToken]; 
[editor save];

在应用程序委托中。然后,当您希望您的用户device1向另一个device2发送推送时,假设您已经以某种方式向device2发送了device1的自定义id,您可以使用该自定义id将推送通知有效载荷发送到Batch.com's API,Batch将处理服务器端的东西到Apple APN,并且您的推送通知出现在device2上。