向主题发送消息 - Firebase iOS

Posted

技术标签:

【中文标题】向主题发送消息 - Firebase iOS【英文标题】:Send message to topic - Firebase iOS 【发布时间】:2017-09-24 05:07:52 【问题描述】:

我想从我的客户端应用程序向使用 Firebase 订阅特定主题的其他设备发送消息。 Firebase 文档告诉我如何发送此请求以部署此消息:

主题 HTTP POST 请求 发送到单个主题:

https://fcm.googleapis.com/fcm/send  
Content-Type:application/json  
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

  
  "to" : /topics/foo-bar",  
  "priority" : "high",  
  "notification" :   
    "body" : "This is a Firebase Cloud Messaging Topic Message!",  
    "title" : "FCM Message",  
    

我不知道如何在我的代码中实现这一点,我尝试过但没有成功,这是我的代码:

NSDictionary *dic1 = @
                       @"body : "  : @"This is a Firebase Cloud Messaging Topic Message",
                       @"title : " : @"FCM Message"
                       ;

NSDictionary *dic2 = @
                      @"to : " : @"/topics/news",
                      @"priority : " : @"high",
                      @"notification : " : dic1

                      ;

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://fcm.googleapis.com/fcm/send"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"key=MYKEY" forHTTPHeaderField:@"Authorization"];

NSError *error;
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:dic1 options:NSJSONWritingPrettyPrinted error:&error]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

请帮忙。 谢谢

【问题讨论】:

嘿,你有解决方案吗? 【参考方案1】:

我找到了问题的解决方案。

正确的代码如下:

NSDictionary *dic1 = @
                       @"body"  : @"This is a Firebase Cloud Messaging Topic Message",
                       @"title" : @"FCM Message"
                       ;

NSDictionary *dic2 = @
                       @"to" : @"/topics/news",
                       @"priority" : @"high",
                       @"notification" : dic1
                       ;

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://fcm.googleapis.com/fcm/send"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"key=YOURKEY" forHTTPHeaderField:@"Authorization"];
NSError *error;
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:dic2 options:NSJSONWritingPrettyPrinted error:&error]];
[[[NSURLSession sharedSession] dataTaskWithRequest:request] resume];

我希望这将有助于将来解决此问题的任何人。

【讨论】:

您能否解释一下代码为什么/如何正确?这可能有助于未来的人们更好地理解问题。 @mech 你必须把“body”这个词后面和“title”这个词后面的双点去掉【参考方案2】:

主题通知发送方法

class PushNotificationSender 


public static let shared = PushNotificationSender()
func sendPushNotification(to token: String, title: String, body: String) 
    let urlString = "https://fcm.googleapis.com/fcm/send"
    let url = NSURL(string: urlString)!
    let paramString: [String : Any] = ["to" : "/topics/Post",
                                       "notification" : ["title" : title, "body" : body],
                                       "data" : ["user" : "test_id"]
    ]

    let request = NSMutableURLRequest(url: url as URL)
    request.httpMethod = "POST"
    request.httpBody = try? JSONSerialization.data(withJSONObject:paramString, options: [.prettyPrinted])
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
       request.setValue("key= set your server key find from your firbase cloud messging setting", forHTTPHeaderField: "Authorization")

    let task =  URLSession.shared.dataTask(with: request as URLRequest)   (data, response, error) in
        do 
            if let jsonData = data 
                if let jsonDataDict  = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] 
                    NSLog("Received data:\n\(jsonDataDict))")
                
            
         catch let err as NSError 
            print(err.debugDescription)
        
    
    task.resume()
  

用法

  PushNotificationSender.shared.sendPushNotification(to: "" , title: fullName , body: textView.text)

【讨论】:

以上是关于向主题发送消息 - Firebase iOS的主要内容,如果未能解决你的问题,请参考以下文章

Firebase 函数向主题发送消息

一次向多个(超过 3 个)主题发送 Firebase 云消息(又名推送通知)

使用用户的UID发送Firebase云消息

Firebase 消息:由于所需的 APNs SSL 证书已过期或未上传,无法向 iOS 设备发送消息

如何向主题发送通知

是否跨 Firebase 项目共享云消息传递主题?