简体   繁体   English

ios使用GCM设备向设备发送推送通知

[英]ios send push notification with GCM Device to Device

I want to be able to send push notification to a topic from device to device. 我希望能够将推送通知发送到设备之间的主题。 I am already able to receive the notifications if I send them from a server but in the end I don't want a server to interact with my apps. 如果我从服务器发送通知,我已经可以接收到通知,但最后我不希望服务器与我的应用进行交互。

I wrote a method that send the notification that look like this : 我编写了一种发送通知的方法,如下所示:

-(void)sendNotif {
    NSDictionary *message = @{
                          @"notification" : @"{ \"text\" : \"test\", \"title\" : \"test\"}",
                          @"to" : @"/topics/test"
                          };
    // kSenderID is the senderID you want to send the message to
    NSString *kSenderID = @"X";
    NSString *to = [NSString stringWithFormat:@"%@@gcm.googleapis.com", kSenderID];
    DLOG(@"dict %@,  to : %@",message, to);
    [[GCMService sharedInstance] sendMessage:message to:to withId:@"id1"];   
}

But it seems nothing is sent. 但是似乎什么也没发送。

So I have 2 questions : How do I write my method? 所以我有2个问题:如何编写方法? How do I implement the callback methods? 如何实现回调方法?

A solution I found is to create my own HTTPRequest like in google example : 我发现的一个解决方案是创建自己的HTTPRequest,如google示例所示:

-(void)sendNotif {
NSString *sendUrl = @"https://android.googleapis.com/gcm/send";
NSString *subscriptionTopic = @"/topics/test";
NSString *title = notifTitle.text;
NSString *body = notifBody.text;
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:sendUrl ]];
req.HTTPMethod = @"POST";
[req setValue:@"application/json" forHTTPHeaderField: @"Content-Type"];
[req setValue:@"key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" forHTTPHeaderField: @"Authorization"];
NSDictionary *message = [self getMessageTo:subscriptionTopic withTitle:title withBody:body];
NSError *jsonError;
NSMutableString *jsonString;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:message options:NSJSONWritingPrettyPrinted error:&jsonError];
if (! jsonData) {
    NSLog(@"Got an error: %@", jsonError);
} else {
     jsonString = [[NSMutableString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
DLOG(@"json string%@", jsonString);
req.HTTPBody = jsonData;
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        if (error != nil) {
            DLOG(@"truc error %@",error);
        } else {
            DLOG(@"Success! Response from the GCM server:");
            DLOG(@"%@",response);
        }
    }];
}

-(NSDictionary *) getMessageTo:(NSString *) to withTitle:(NSString *)   title withBody:(NSString *) body{
// [START notification_format]
NSDictionary *message = @{
                          @"notification" : @{@"title" : title,@"text" : body},
                          @"to" : to
                          };
return message;
// [END notification_format]
}

There is no normal way to publish a message to topic from the client. 没有正常的方法可以从客户端向主题发布消息。 The one proposed by the question author him/herself is basically a hack that requires keeping the API key on the client which is very insecure. 问题作者自己提出的建议基本上是一种黑客,需要将API密钥保留在客户端上,这是非常不安全的。

The main reason why it is not allowed is that it would let adversary to tamper with the client and send spam messages to other users. 不允许这样做的主要原因是,它会让对手篡改客户端并向其他用户发送垃圾邮件。

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

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