ios send push notification with GCM Device to Device

761 Views Asked by At

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? How do I implement the callback methods?

2

There are 2 best solutions below

1
On

A solution I found is to create my own HTTPRequest like in google example :

-(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]
}
0
On

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.

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.