I am facing an issue where I am able to successfully send notifications to my iOS device through the FCM console, but I am unable to do

I am experiencing an issue with FCM (Firebase Cloud Messaging) notifications. While I can successfully send notifications through the FCM console to my iOS device, it does not work when sending notifications from a PHP service. My goal is to be able to send notifications to my app using FCM. I have already implemented web services in PHP for this purpose, which consists of 4 services:

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

After creating a group, I was able to obtain a notification key and register a registration ID with FCM. However, when I call the "send_comments.php" service with the notification key, it returns JSON data with {"success":1,"failure":0}, but I do not receive any notification on my iOS device. I have double-checked that all methods are properly implemented. The FCM console works fine, but the PHP service does not. Does anyone know what could be causing this issue? I have attached all 4 PHP files below. Any help would be greatly appreciated.

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 );
?>

Answer №1

The structure of APNs (Apple Push Notification service) messages consists of a specific format:

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

To clarify, the following is an example format:

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

Only if this format is used for sending notifications from the server side will they be displayed on iPhones. Otherwise, only the notification data will be printed in the console using the method below:

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

However, they will not appear in the list of notifications.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The PHP content form is malfunctioning and not processing email submissions properly, resulting in the PHP code being

Following a tutorial, I attempted to create a basic contact form using PHP. As a beginner in PHP, I found the process fairly straightforward, but encountered some bugs in the code. Upon integrating the code on my website, clicking the submit button redirec ...

Is it possible to implement an authentication guard in Angular and Firebase to verify if the user is currently on the login page?

My Current Tools Using Angular and Firebase My Objective To implement an Auth Guard that can redirect users based on specific conditions Current Progress I have set up an auth guard on various components which redirects users back to the login page ...

What is the best way to send information containing the + symbol in an AJAX request?

I'm currently sending data to the server via an AJAX call using the code snippet below: var data = "productid="+productid+"&title="+title; $.ajax({ type: "POST", url: url + "target.php", data: data, dataType: "json"}) However, the title var ...

There is no valid injection token found for the parameter 'functions' in the class 'TodosComponent'

While working in my code, I decided to use 'firebase' instead of '@angular/fire'. However, I encountered an issue that displayed the following error message: No suitable injection token for parameter 'functions' of class &apos ...

Is it possible to have nested associations within Shopware 6?

Within my EntityDefinition, there is an association: ... class ParentEntityDefinition extends EntityDefinition { ... protected function defineFields(): FieldCollection { return new FieldCollection([ (new OneToOn ...

Leveraging PHP, AJAX, and MySQL within the CodeIgniter framework

I am currently venturing into the world of web development using the codeigniter PHP framework. My current hurdle involves a select dropdown on a page, populated with unique IDs such as 1, 2, 3 from the database. What I aim to achieve is, when a value is s ...

What could be the cause of the error I am encountering in my PHP and MySQL environment?

My goal is to determine the number of rows that contain values for both "referral_in" and "referral_out" as separate variables. Below is the code I have written: $username = $_SESSION['username']; $connect = mysql_connect("xxxx", "xxxx", "xxxx! ...

What can I do to stop hidden HTML tags from loading on my page? Is it possible to completely remove them from the page

I previously inquired about this question but did not receive a satisfactory answer. Here are the input fields I am working with: <form method ="post" action="select.php"> <input id="nol" style="width: 350px;" type="text" name="searchdisease" pl ...

What is the process for transferring a file to a different directory?

<html> <head> <title>Main Page</title> </head> <body> <h2>Main Page</h2> <form method="post" action="index.php" enctype="multipart/form-data"> <input type="file" name="filename"> <input type=" ...

Tips for transferring data to the next page with JavaScript AJAX

I am working on a webpage that includes an html select element <pre> $query = mysql_query("select * from results"); echo "<select id='date' onchange='showdata()' class='form-control'>"; while ($arr = mysql_fetch_a ...

My .htaccess file seems to be ineffective, as if it doesn't even exist

Well, I'm feeling pretty frustrated right now. I have a basic php website and all I wanted to do was remove the page extension (index.php to index). According to guides I read, all I needed to do was create a .htaccess file and add this code to it. R ...

Tips to prevent redirection in a JavaScript function

When a user clicks on a specific link, the HideN function is triggered. Here's an example: <a href="<?php echo $dn5['link']; ?>" onclick="HideN('<?php echo $dn5['id'];?>','<?php echo $dn5['fro ...

Wordpress tabs with dynamic content

I came across a code on webdeveloper.com by Mitya that had loading content tabs and needed the page to refresh after clicking the tab button. It worked perfectly fine outside of WordPress, but when I tried implementing it into my custom theme file, it didn ...

What is the best way to incorporate this json feed into a database format?

Working on developing an app where I am integrating various stores for customers to purchase products. I have a json feed containing all the necessary data about these stores. Many fields like phone number, address, and postcode are straightforward to inc ...

Making a secure connection using AJAX and PHP to insert a new row into the database

Explaining this process might be a bit tricky, and I'm not sure if you all will be able to assist me, but I'll give it my best shot. Here's the sequence of steps I want the user to follow: User clicks on an image (either 'cowboys&apos ...

The function wp_get_current_user() is not defined within capabilities.php and cannot be called

After neglecting my website for an extended period of time, I was shocked to discover that it had suddenly stopped working. A quick investigation led me to the error_log file where I found the following fatal error message: [18-Mar-2014 19:16:43 UTC] PHP ...

Ensuring that links are functional across all directory levels: a comprehensive guide

Looking at the image included here, you can see a preview of the website I'm currently building. The plan is to have individual pages for each machine in the company's lineup. Since the navigation bar and menu will be consistent across the entire ...

Is XMLHttpRequest just sitting idle...?

As a newcomer to the world of javascript and AJAX, I've been stuck on a single problem for the past 8 hours. Despite my best efforts, I just can't seem to figure out what's going wrong. On my website, I have an image with an onclick event ca ...

Failure to trigger AJAX Success or Error Events

I'm struggling to understand why this code isn't working or find any relevant resources. When I check the json object in Firebug, it either returns success: false or success: true from the post request, so I'm confused as to why the function ...

Issues encountered with PHP and MySQL query functions

I have a database filled with information that I want to display on my website. In order to accomplish this, I am creating a function called "the" for each piece of data: $profileMeta = mysql_query("SELECT * FROM profiles WHERE profileID = '$profileI ...