2016-06-06 3 views
4

iOS 기기에서 다른 iOS 기기로 메시지를 보내고 Firebase 알림과 Google Cloud Messaging 간의 차이점을 이해하는 방법을 알지 못했습니다.Firebase Google Cloud 기기에서 기기로 메시지 교환

Firebase 알림는 서버에서 기기로 메시지를 보낼 수 있다고 말합니다.

Google 클라우드 메시징 : 서버에서 기기 (다운 스트림) 또는 기기 대 서버 (업스트림)로 메시지를 전송합니다 !! 상류의

예 :

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

내가 뭘 장치에 장치에서 푸시 메시지를 전송해야하는 경우에 대한! 그것은 장치가 서버에 메시지를 보낸 후에, 나는 firebase 서버를 프로그래밍하여 클라이언트에 푸시를 보내야한다는 것을 의미합니까? 그게 정말로 혼란스러워!

+0

장치에 장치를 보내려면 흐름에 대해 정확해야합니다. 서버로 업스트림을 보내면 서버가 다운 스트림을 다른 장치로 보냅니다. –

답변

1

firebase를 사용하는 iOS에서는이 작업을 수행 할 수 없습니다. 파이어베이스에서 서비스를 호출하면 다른 장치에 알림이 전송됩니다. APNS와 GCM은 서버 설정 측면에서 약간 다릅니다.

GCM의 경우 서버 호출, 휴대 기기 등 어느 곳에서나 수행 할 수있는 https://android.googleapis.com/gcm/send으로 POST 호출에 API 키를 추가하기 만하면됩니다. 대상 기기 토큰 및 API 키만 있으면됩니다.

APNS는 다르게 작동합니다. Apple 개발자 포털에서 만든 서버 SSL 인증서를 첨부해야 자신을 인증하고 기기에 푸시 알림을 보낼 수 있습니다. iOS 기기에서 어떻게이 작업을 수행 할 수 있었는지 잘 모르겠습니다.

이 스레드는 GCM과 중포 기지 사이의 진정한 차이를 명확히,

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

중포 기지와 GCM은 다르지만 그들은 같은 목표를 달성하는 데 사용할 수 있습니다

Real-time Push notifications with Firebase

. 희망이 당신을 돕는다.

0

현재 Firebase가이 시나리오를 처리 할 준비가되어 있다고 생각하지 않습니다. 그것을 처리하기 위해서는 약간의 사이드 코드가 필요합니다. 어느 쪽이든 당신은 호스팅 얻을 작동, 또는 당신이 백엔드 역할을 할 수있는 또 다른 서비스를 찾을 필요가

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

코드를 통합하는 데 사용 할 수있는 PHP를 엔드 포인트처럼 만들 수 있습니다.

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를 보내기 위해 작성한 실제 코드입니다. Batch에서 다운로드 할 수있는 Swift 2 및 Swift 3 예제가 있습니다.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로 설치하기가 매우 쉽습니다.

애플 대리자에서 :

@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 웹 사이트에서 설명하는 설치 및 설치 작업 외에도 작업 방법을 설명합니다. 당신이 중포 기지에서 토큰을 일단

, 당신은 기본적으로 앱 위임에

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

와 배치에 등록합니다. 그런 다음 사용자 device1이 다른 device2에 Push를 보내도록하려는 경우 device1의 사용자 정의 id를 device2에 어떻게 든 보냈다고 가정하면 해당 사용자 정의 ID를 사용하여 Push notification 페이로드를 Batch.com의 API로 보내고 Batch가 서버를 처리 할 수 ​​있습니다 Apple APN에 사이드 항목이 표시되고 푸시 알림이 device2에 나타납니다.

관련 문제