Firebase Cloud Messaging android project doesn't send push notifications
<p>I am trying to develop an application, in which a server must send notifications to all android devices running that application at every 5 seconds. I decided to use (Google) Firebse Cloud Messaging for sending the notifications, so I tried the example project of the guide first </p>

<p><a href="https://firebase.google.com/docs/notifications/android/console-audience" rel="nofollow">https://firebase.google.com/docs/notifications/android/console-audience</a></p>

<p>but I cannot make it work. I followed all the instructions. I have posted the code I used. I also did <strong>file-->project structure-->notifications-->checked the box Google Cloud Messaging</strong>. I tried the application at an android 5 device. </p>

<p>When I open the Firebase console and send the notification, I see the refreshed token at the log (from <code>MyFirebaseInstanceIDService</code> class, method <code>onTokenRefresh()</code>), but when I run it again, and send notification to a single device by copying and  pasting the token, nothing happens. Also, when I send notification from the console to <strong>user segment</strong>, nothing happens again. </p>

<p>I also tried the corresponding ios example project for Firebase, from the same website, and it worked fine for the iphone(all the notifications were sent).Am I missing something here?</p>

<pre><code>public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "MyFirebaseIIDService";

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    sendRegistrationToServer(refreshedToken);
}

private void sendRegistrationToServer(String token) {
    // TODO: Implement this method to send token to your app server.
}
}
</code></pre>

<p>MyFirebaseMessagingService :</p>

<pre><code>public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());

    //Toast.makeText(getApplicationContext(),"FROM "+remoteMessage.getFrom(),Toast.LENGTH_LONG).show();
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() &gt; 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        //Toast.makeText(getApplicationContext(),"MESSAGE: "+remoteMessage.getData(),Toast.LENGTH_LONG).show();
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        //MainActivity.TestMethod();
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        //Toast.makeText(getApplicationContext(),"MESSAGE: "+remoteMessage.getData(),Toast.LENGTH_LONG).show();
    }

    // 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.
 sendNotification(remoteMessage.getNotification().getBody());
}

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.drawable.ic_stat_ic_notification)
            .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>main activity:</p>

<pre><code>public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            String value = getIntent().getExtras().getString(key);
            Log.d(TAG, "Key: " + key + " Value: " + value);
        }
    }

    Button subscribeButton = (Button) findViewById(R.id.subscribeButton);
    subscribeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // [START subscribe_topics]
            FirebaseMessaging.getInstance().subscribeToTopic("news");
            // [END subscribe_topics]

            // Log and toast
            String msg = getString(R.string.msg_subscribed);
            Log.d(TAG, msg);
            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });

    Button logTokenButton = (Button) findViewById(R.id.logTokenButton);
    logTokenButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Get token
            String token = FirebaseInstanceId.getInstance().getToken();

            // Log and toast
            String msg = getString(R.string.msg_token_fmt, token);
            Log.d(TAG, msg);
            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });
}
}
</code></pre>

<p>and the build.gradle (project messaging):</p>

<pre><code>    // Top-level build file where you can add configuration options commonto             `all sub-projects/modules.`

buildscript {
    repositories {
        jcenter()
        mavenLocal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        mavenLocal()
    }
}
</code></pre>

<p>and the build.gradle (Module app):</p>

<pre><code>apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.google.firebase.quickstart.fcm"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Testing dependencies
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
    androidTestCompile 'com.android.support.test:runner:0.2'
    androidTestCompile 'com.android.support:support-annotations:24.2.0'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.google.firebase:firebase-messaging:9.4.0'
    compile 'com.google.firebase:firebase-core:9.4.0'
    compile 'com.google.android.gms:play-services-gcm:9.4.0'
    compile 'com.android.support:design:24.2.0'
}

apply plugin: 'com.google.gms.google-services'
</code></pre>

<p>...and here is the manifest file:</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.google.firebase.quickstart.fcm"&gt;

    &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
    &lt;application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"&gt;
        &lt;activity
            android:name=".MainActivity"
            android:label="@string/app_name"&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;!-- [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=".ResultActivity"
            android:label="@string/title_activity_result"
            android:theme="@style/AppTheme.NoActionBar"&gt;&lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;
</code></pre>