0

FCM sending notification through fcm console to my ios device but from php service it is not sending the notification . I want to send notification to my app using FCM . I have implemented web services in php to send message to app on my app server. I created 4 services for this purpose.

  1. group_create.php
  2. device_add.php
  3. device_remove.php
  4. send_comments.php

after creating group i got a notification key successfully and register a registraion id with fcm . when i call send_comments.php with notification key , it returns json data with {"success":1,"failure":0} . But i didn't get any notification on my ios. I have implemented all methods properly. It working well with fcm console but not with php service. Can anyon eknow about this. I am attaching all 4 php files with it. Please help me.

group_create.php

<?php 
$url = 'https://android.googleapis.com/gcm/notification';

$notification_key_name = $_REQUEST['notification_key_name'];
$regid = $_REQUEST['regid'];
    $fields = array(
       "operation"=>"create",
       "notification_key_name"=>$notification_key_name,
       "registration_ids"=> array($regid)
);
$fields = json_encode( $fields );

$headers = array (
        "Authorization:key=A************************",
        "Content-Type:application/json",
        "project_id:78508******"
);

$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

$result = curl_exec ( $ch );

//echo $result;
$res_notification_key = json_decode($result,true);

 if(array_key_exists('notification_key', $res_notification_key)){

$notification_key = $res_notification_key['notification_key'];

 echo $notification_key;

}

else{

echo $result;

}
curl_close ( $ch );
?>

device_add.php

<?php

$senderId = "785********";
$notification_key_name= $_REQUEST['notification_key_name'];
$reg_id = $_REQUEST['regid'];
$notification_key = $_REQUEST['not_key'];
$apiKey = 

$url = 'https://android.googleapis.com/gcm/notification';

  $headers = array (
        "Accept:application/json",
        "Authorization:key=A******************",
        "Content-Type:application/json",
        "project_id:78508*****"
);


 $fields = array(
       "operation"=>"add",
       "notification_key_name"=> $notification_key_name,
       "registration_ids"=> array($reg_id),
       "notification_key"=>$notification_key
);
$fields = json_encode( $fields );

 $ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );


$result = curl_exec ( $ch );

echo $result;
$res_notification_key = json_decode($result,true);

if(array_key_exists('notification_key', $res_notification_key)){


$notification_key = $res_notification_key['notification_key'];

 echo $notification_key;

}

else{

echo $result;

}
curl_close ( $ch );
?>

device_remove.php

<?php

$senderId = "78508*****";
$notification_key_name= $_REQUEST['notification_key_name'];
$reg_id = $_REQUEST['regid'];
$notification_key = $_REQUEST['not_key'];
$apiKey = 

$url = 'https://android.googleapis.com/gcm/notification';

  $headers = array (
        "Accept:application/json",
        "Authorization:key=A***********",
        "Content-Type:application/json",
        "project_id:78508*****"
);


 $fields = array(
       "operation"=>"remove",
       "notification_key_name"=> $notification_key_name,
       "registration_ids"=> array($reg_id),
       "notification_key"=>$notification_key
);
$fields = json_encode( $fields );

 $ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );


$result = curl_exec ( $ch );

echo $result;
$res_notification_key = json_decode($result,true);



if(array_key_exists('notification_key', $res_notification_key)){


$notification_key = $res_notification_key['notification_key'];

 echo $notification_key;

}

else{

echo $result;

}
curl_close ( $ch );
?>

send_comments.php

<?php

$senderId = "78508*****";
$notification_key = $_REQUEST['not_key'];

$url = 'https://fcm.googleapis.com/fcm/send';

  $headers = array (
        "Authorization:key=A*****************",
        "Content-Type:application/json",


);

$msg = array("hello"=>"This is a Firebase Cloud Messaging Device Group Message!");

$msg_dict = json_encode($msg);

//echo $msg_dict;

 $fields = array(
       "to"=>$notification_key,
       "data"=>array(
            "message" => "hell",

    ),
);
$fields = json_encode( $fields );

 $ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

$result = curl_exec ( $ch );

echo $result;
$res_notification_key = json_decode($result,true);

curl_close ( $ch );
?>

1 Answers1

0

APNs follows a format

{"aps":{"alert":"Testing.. (0)","badge":1,"sound":"default"}}

i.e.,

{
"aps":{
"alert":"message here"
}
}

if the format sending from server side is this,then only iPhone will display notifications,other wise notification data will only print in console through

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage         *)remoteMessage {
// Print full message
NSLog(@"Notification :%@", remoteMessage.appData);
}

but not display in notifications list.

Jeevan
  • 1