【问题标题】:Firebase Google Cloud messaging from device to device从设备到设备的 Firebase Google Cloud 消息传递
【发布时间】:2016-06-06 10:37:53
【问题描述】:

我不明白如何将消息从 iOS 设备发送到另一台 iOS 设备,并试图了解两者之间的区别 Firebase 通知和 Google 云消息传递。

Firebase 通知表示您可以从服务器向设备发送消息。

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

上游示例:

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

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

【问题讨论】:

  • 您对将设备发送到设备的流程是正确的。将上游发送到您的服务器,然后服务器将下游发送到其他设备。

标签: ios firebase firebase-cloud-messaging


【解决方案1】:

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

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

APNS 的工作方式不同,您需要附加您在 Apple 开发人员门户上创建的服务器 SSL 证书以验证您自己并向设备发送推送通知。我不确定您如何在 iOS 设备上实现这一点。

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

Real-time Push notifications with Firebase

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

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

【讨论】:

    【解决方案2】:

    我认为 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 上。

    【讨论】:

      猜你喜欢
      • 2016-11-17
      • 2016-09-22
      • 2016-10-30
      • 2017-01-25
      相关资源
      最近更新 更多