title: Firebase showing success but not sending cloud messaging (Android - PHP)
body: <p>I've done a lot of research to figure out why it's causing the problem, but I still can not figure out why.</p>
  
  <p>I can send notifications from console. I can send notifications to one device using token from console.</p>
  
  <p>But i couldn't send cloud messaging from PHP.</p>
  
  <p>Also i edited build.gradle and add google-services.json given firebase.</p>
  
  <p>Android Manifest</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.agebilisim.clooger"&gt;
  
      &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.READ_CONTACTS" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.READ_PHONE_STATE"/&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
  
          &lt;!-- [START fcm_default_icon] --&gt;
          &lt;!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
               See README(...) for more. --&gt;
          &lt;!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
               notification message. See README() for more. --&gt;
          &lt;meta-data
              android:name="com.google.firebase.messaging.default_notification_color"
              android:resource="@color/colorAccent" /&gt;
          &lt;!-- [END fcm_default_icon] --&gt;
  
          &lt;activity
              android:name=".MainActivity"
              android:theme="@style/AppTheme.NoActionBar"
              android:screenOrientation="portrait"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".cihazlisteleme"
              android:label="Cihazlarınız"
              android:screenOrientation="portrait"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.CIHAZLISTELEME" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".tamara"
              android:screenOrientation="portrait"
              android:label="Cihaz Ekleyin"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.TARAMA" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".cihaz_kayit"
              android:screenOrientation="portrait"
              android:label="Cihazınızı Tanımlayın"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.CIHAZ_KAYIT" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".sensor_gorunum"
              android:screenOrientation="portrait"
              android:label="@string/title_activity_sensor_gorunum"
              android:theme="@style/AppTheme.NoActionBar"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.SENSORGORUNUM" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".grafik"
              android:screenOrientation="landscape"
              android:theme="@style/AppTheme.NoActionBar"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.GRAFIK" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".ayarkayit"
              android:label="Sensör Ayarları"
              android:screenOrientation="portrait"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.AYAR_KAYIT" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".genel_ayarlar"
              android:label="Terminal Ayarları"
              android:screenOrientation="portrait"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.GENEL_AYARLAR" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".CONNECT_WIFI"
              android:label="Clogger Wifi Yapılandırma"
              android:screenOrientation="portrait"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.CONNECT_WIFI" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".CLOGGER_BAGLANTI"
              android:label="Clogger Wifi Yapılandırma"
              android:screenOrientation="portrait"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.CLOGGER_BAGLANTI" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".CLOGGER_BAGLANTI_SUNUM"
              android:theme="@style/AppTheme.NoActionBar"
              android:screenOrientation="portrait"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.CLOGGER_BAGLANTI_SUNUM" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity android:name=".TABLO_BUGUN"
              android:screenOrientation="portrait"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.TABLO_BUGUN" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;!-- [START firebase_service] --&gt;
          &lt;service
              android:name=".MyFirebaseMessagingService"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.google.firebase.MESSAGING_EVENT"/&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
          &lt;!-- [END firebase_service] --&gt;
          &lt;!-- [START firebase_iid_service] --&gt;
          &lt;service
              android:name=".MyFirebaseInstanceIDService"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.google.firebase.INSTANCE_ID_EVENT"/&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
          &lt;!-- [END firebase_iid_service] --&gt;
  
          &lt;activity android:name=".ALARM"
              android:screenOrientation="portrait"&gt;&lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>MyFirebaseMessagingService.java</p>
  
  <pre><code>public class MyFirebaseMessagingService extends FirebaseMessagingService  {
  
      private static final String TAG = "MyFirebaseMsgService";
  
      /**
       * Called when message is received.
       *
       * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
       */
      // [START receive_message]
      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
          // [START_EXCLUDE]
          // There are two types of messages data messages and notification messages. Data messages are handled
          // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
          // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
          // is in the foreground. When the app is in the background an automatically generated notification is displayed.
          // When the user taps on the notification they are returned to the app. Messages containing both notification
          // and data payloads are treated as notification messages. The Firebase console always sends notification
          // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
          // [END_EXCLUDE]
  
          // TODO(developer): Handle FCM messages here.
          // Not getting messages here? See why this may be: 
          Log.d(TAG, "From: " + remoteMessage.getFrom());
  
          // Check if message contains a data payload.
          if (remoteMessage.getData().size() &gt; 0) {
              Log.d(TAG, "Message data payload: " + remoteMessage.getData());
          }
  
          // Check if message contains a notification payload.
          if (remoteMessage.getNotification() != null) {
              Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
          }
  
          // Also if you intend on generating your own notifications as a result of a received FCM
          // message, here is where that should be initiated. See sendNotification method below.
      }
      // [END receive_message]
  
      /**
       * Create and show a simple notification containing the received FCM message.
       *
       * @param messageBody FCM message body received.
       */
      private void sendNotification(String messageBody) {
          Intent intent = new Intent(this, MainActivity.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                  PendingIntent.FLAG_ONE_SHOT);
  
          Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                  .setSmallIcon(R.id.action_settings)
                  .setContentTitle("FCM Message")
                  .setContentText(messageBody)
                  .setAutoCancel(true)
                  .setSound(defaultSoundUri)
                  .setContentIntent(pendingIntent);
  
          NotificationManager notificationManager =
                  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
          notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
      }
  }
  </code></pre>
  
  <p>MyFirebaseInstanceIDService.java</p>
  
  <pre><code>    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService{
  
  
      String url_goster   =   "http://185.126.217.71/firebase.php";
      RequestQueue requestQueue;
  
      private static final String TAG = "MyFirebaseIIDService";
  
      @Override
      public void onTokenRefresh() {
          String token = FirebaseInstanceId.getInstance().getToken();
          Log.d(TAG, "Token: " + token);
  
          sendRegistrationToServer(token);
      }
  
      private void sendRegistrationToServer(final String token) {
          // token'ı servise gönderme işlemlerini bu methodda yapmalısınız
  
          requestQueue= Volley.newRequestQueue(getApplicationContext());
  
          //İNTERNETE BAĞLANIP BİLGİLERİ ÇEKİYORUZ
  
          StringRequest request= new StringRequest(Request.Method.POST, url_goster, new Response.Listener&lt;String&gt;() {
              @Override
              public void onResponse(String response) {
  
  
              }
          }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
  
              }
          }){
  
              @Override
              protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
  
                  Map&lt;String,String&gt; params = new HashMap&lt;String,String&gt;();
  
                  params.put("FIREBASE",token);
  
  
                  return params;
              }
          };
  
          requestQueue.add(request);
  
      }
  
  }
  </code></pre>
  
  <p>I tried two different PHP code.</p>
  
  <p>First I used this code:</p>
  
  <pre><code>&lt;?php
  
  // Call .php?Action=M&amp;t=title&amp;m=message&amp;r=token
  $action=$_GET["Action"];
  
  
  switch ($action) {
      Case "M":
           $r=$_GET["r"];
          $t=$_GET["t"];
          $m=$_GET["m"];
  
          $j=json_decode(notify($r, $t, $m));
  
          $succ=0;
          $fail=0;
  
          $succ=$j-&gt;{'success'};
          $fail=$j-&gt;{'failure'};
  
          print "Success: " . $succ . "&lt;br&gt;";
          print "Fail   : " . $fail . "&lt;br&gt;";
  
          break;
  
  
  default:
          print json_encode ("Error: Function not defined -&gt;" . $action);
  }
  
  function notify ($r, $t, $m)
      {
      // API access key from Google API's Console
          if (!defined('API_ACCESS_KEY')) define( 'API_ACCESS_KEY', 'i write my firebase server key' );
          $tokenarray = array($r);
          // prep the bundle
          $msg = array
          (
              'title'     =&gt; $t,
              'message'     =&gt; $m,
             'MyKey1'       =&gt; 'MyData1',
              'MyKey2'       =&gt; 'MyData2', 
  
          );
          $fields = array
          (
              'registration_ids'     =&gt; $tokenarray,
              'data'            =&gt; $msg
          );
  
          $headers = array
          (
              'Authorization: key=' . API_ACCESS_KEY,
              'Content-Type: application/json'
          );
  
          $ch = curl_init();
          curl_setopt( $ch,CURLOPT_URL, 'fcm.googleapis.com/fcm/send' );
          curl_setopt( $ch,CURLOPT_POST, true );
          curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
          curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
          curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
          curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
          $result = curl_exec($ch );
          curl_close( $ch );
          return $result;
      }
  
  
  ?&gt;
  </code></pre>
  
  <p>if I use this code output is: </p>
  
  <blockquote>
    <p>Success: 1 Fail : 0</p>
  </blockquote>
  
  <p>And another PHP code:</p>
  
  <pre><code>    &lt;?php
  // API access key from Google API's Console
  define( 'API_ACCESS_KEY', 'AAAAIYUM_dc:APA91bHkh9tuO47OPhPUTfzjMc7tc6Y6vzDNMbxFeOD0sEkEcTXu5Mtiec_er_k5HIWGf8COysQ1xJ071dJEDb0GA4RKoiuKkd0j-nkY3PRcy8fJ-ZuyPsmGetHmZoT4eL6KQ_GNaU0S' );
  $registrationIds = array( $_GET['id'] );
  // prep the bundle
  $msg = array
  (
      'message'   =&gt; 'here is a message. message',
      'title'     =&gt; 'This is a title. title',
      'subtitle'  =&gt; 'This is a subtitle. subtitle',
      'tickerText'    =&gt; 'Ticker text here...Ticker text here...Ticker text here',
      'vibrate'   =&gt; 1,
      'sound'     =&gt; 1,
      'largeIcon' =&gt; 'large_icon',
      'smallIcon' =&gt; 'small_icon'
  );
  $fields = array
  (
      'registration_ids'  =&gt; $registrationIds,
      'data'          =&gt; $msg
  );
  
  $headers = array
  (
      'Authorization: key=' . API_ACCESS_KEY,
      'Content-Type: application/json'
  );
  
  $ch = curl_init();
  curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
  curl_setopt( $ch,CURLOPT_POST, true );
  curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
  curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
  curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
  $result = curl_exec($ch );
  curl_close( $ch );
  echo $result;
  </code></pre>
  
  <p>This code output is: </p>
  
  <blockquote>
    <p>{"multicast_id":5272616625278801394,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1488877477780922%e3ee78bef9fd7ecd"}]}</p>
  </blockquote>
  
  <p>Code showing success but i couldn't get any notification on my phone (Lg G3 Android 6.0)</p>
  
  <p>I am searching from 2 days and still do not know what the problem is :(</p>
  
  <p>And debug console:</p>
  
  <pre><code>    $ adb shell am start -n "com.agebilisim.clooger/com.agebilisim.clooger.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
  Connecting to com.agebilisim.clooger
  Connected to the target VM, address: 'localhost:8600', transport: 'socket'
  I/System.out: waiting for debugger to settle...
  I/System.out: waiting for debugger to settle...
  I/System.out: debugger has settled (1309)
  W/System: ClassLoader referenced unknown path: /data/app/com.agebilisim.clooger-2/lib/arm
  I/InstantRun: Instant Run Runtime started. Android package is com.agebilisim.clooger, real application class is null.
  W/System: ClassLoader referenced unknown path: /data/app/com.agebilisim.clooger-2/lib/arm
  D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
  W/InstanceID/Rpc: Found 10005
  D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
  I/FA: App measurement is starting up, version: 10298
  I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
  V/FA: Collection enabled
  V/FA: App package, google app id: com.agebilisim.clooger, 1:143966141911:android:a4514eaaaa81f5b0
  I/FA: To enable faster debug mode event logging run:
          adb shell setprop debug.firebase.analytics.app com.agebilisim.clooger
  D/FA: Debug-level message logging enabled
  V/FA: Registered activity lifecycle callback
  I/FirebaseInitProvider: FirebaseApp initialization successful
  V/FA: Checking service availability
  V/FA: Service available
  V/FA: Setting useService: true
  V/FA: Using measurement service
  V/FA: Connecting to remote service
  W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
  V/FA: onActivityCreated
  D/PhoneWindowEx: [PWEx][generateLayout] setLGNavigationBarColor : colors=0xfff5f5f5
  I/PhoneWindow: [setLGNavigationBarColor] color=0x fff5f5f5
  I/art: Background sticky concurrent mark sweep GC freed 83588(6MB) AllocSpace objects, 38(856KB) LOS objects, 34% free, 37MB/57MB, paused 519us total 113.859ms
  V/FA: Using measurement service
  V/FA: Connection attempt already in progress
  V/FA: Activity resumed, time: 47042510
  D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
  D/PhoneWindow: notifyNavigationBarColor, color=0x: fff5f5f5, token: android.view.ViewRootImplAO$WEx@2eb93a0
  I/Adreno-EGL: &lt;qeglDrvAPI_eglInitialize:379&gt;: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU (I49f5588d88)
                OpenGL ES Shader Compiler Version: E031.29.00.00
                Build Date: 12/19/15 Sat
                Local Branch: mybranch17333385
                Remote Branch: quic/LA.BF.1.1.3_rb1.6
                Local Patches: NONE
                Reconstruct Branch: NOTHING
  I/OpenGLRenderer: Initialized EGL, version 1.4
  V/RenderScript: 0x99516000 Launching thread(s), CPUs 4
  D/FA: Connected to remote service
  V/FA: Processing queued up service tasks: 2
  V/ViewRootImpl: Contents drawing finished : com.agebilisim.clooger/com.agebilisim.clooger.MainActivity
  I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@9e2026e time:25238089
  D/MyFirebaseIIDService: Token: cds2SIwJXMU:APA91bHAxP479sLz6pCdt2zWXoscfxCNaf6rNG2nE2bItaXoDjd_V_o_zYVQ5iHWSYWrztVUuH76tFL_h00u8iaD5Di1pKEdLTPhhYqaIIjjENaQJBpFrAAhaeej3bcdK0pbEYY-Z4jD
  W/System: ClassLoader referenced unknown path: /system/framework/tcmclient.jar
  V/FA: Inactivity, disconnecting from the service
  V/FA: Session started, time: 47052536
  I/FA: Tag Manager is not found and thus will not be used
  D/FA: Logging event (FE): _s, Bundle[{_o=auto, _sc=MainActivity, _si=-7538718783108208390}]
  V/FA: Using measurement service
  V/FA: Connecting to remote service
  D/FA: Connected to remote service
  V/FA: Processing queued up service tasks: 1
  V/FA: Inactivity, disconnecting from the service
  I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
  I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
  D/MyFirebaseMsgService: From: 143966141911
  D/MyFirebaseMsgService: Message data payload: {subtitle=This is a subtitle. subtitle, smallIcon=small_icon, sound=1, title=This is a title. title, vibrate=1, largeIcon=large_icon, message=here is a message. message, tickerText=Ticker text here...Ticker text here...Ticker text here}
  </code></pre>