繁体   English   中英

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

[英]Firebase Google Cloud messaging from device to device

我无法理解如何将消息从iOS设备发送到另一台iOS设备,并尝试了解Firebase Notifications与Google Cloud Messaging之间的区别。

Firebase Notifications说明来自服务器,您可以向设备发送消息。

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

上游示例:

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

如果我需要从设备发送推送消息怎么办! 这是否意味着在设备向服务器发送消息后,我必须对firebase服务器进行编程以将推送发送到客户端? 它真的很混乱!

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

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

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

该主题阐明了GCM和Firebase之间的真正区别,

使用Firebase进行实时推送通知

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

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

我认为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特别是推送通知公司,似乎免费的基本计划足以处理你需要的东西,对于Parse如何运作起到了很大的作用。

这是我为发送推送通知Objective C编写的实际代码。(还可以从Batch.com下载Swift 2和Swift 3示例)

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。

我还使用此代码来使其工作:

在app委托中:

@import Batch

在didFinishLaunching中:

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

然后在app委托:

- (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 with上注册它

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

在App Delegate中。 然后,当您希望用户device1将Push发送到另一个设备2时,假设您已经以某种方式将device1的自定义ID发送到device2,您可以使用该自定义ID将推送通知有效负载发送到Batch.com的API,Batch将处理服务器苹果APN的副作用,你的推送通知出现在device2上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM