The code I already have running but only receives the notification if the app is open or in the background, not if it is closed. I'm looking for a lot but I can not find the solution.
I do not know if it will depend on something from the Manifest:
<service
android:name=".FirebaseMessagingServ">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".FirebaseInstanceIDServ">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
The FirebaseMessagingServ file is responsible for removing the notification:
public class FirebaseMessagingServ extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message){
String from = message.getFrom();
Map data = message.getData();
showNotification(message.getData().get("message"));
}
private void showNotification(String sms){
Intent i = new Intent(this, Main_Principal.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("FCM test")
.setContentText(sms)
.setSmallIcon(R.drawable.icon_app_grande)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
Which works perfectly as long as the app is open or in the background, it does not work when it's closed.
The class FirebaseInstanceIDServ only takes care of extracting the Token and sending it to the Sql.
My problem for the comments is in the System tray, in case I had something to do this is my php in charge of sending:
<?php
//Conectamos BD
include("conn.php");
$con = conectar();
function send_notification ($tokens,$message, $notification){
$url = 'http://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message,
'notification' => $notification);
$headers = array(
'Authorization:key = AIzaSyB_317WEFmazhxxxxxxxxxma3o81qjA',
'Content-Type: application/json'
);
$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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$sql = "SELECT token From fcm_tokens";
$result = mysql_query($sql,$con);
$tokens = Array();
if(mysql_num_rows($result)>0){
while($row = mysql_fetch_assoc($result)){
$tokens[] = $row["token"];
}
}
mysql_close($con);
$message = array("message" => "Prueba de FCM");
$notification = array("message" => "Prueba de FCMd", "body" => "cuerpooo", "icon" => "ic_launcher");
$message_status = send_notification($tokens, $message,$notification);
echo $message_status;
?>
When the app is active the information of the 'Data' arrives and when it is in the background the information of the 'notification' arrives, but when the app is closed (process killed) no type of notification arrives.
Should I make any changes?
Last minute news: The tests are doing this on my mobile Xiaomi, I just tried it on Nexus 5 and there if they arrive even if the app is closed, that only from the console does not des of PHP. Does anyone know the explanation?
Thank you very much.