Quickstart-android: Android (sdk 26) Firebase messaging is not working

Created on 4 May 2018  路  5Comments  路  Source: firebase/quickstart-android

I have been trying to implement Firebase messaging in my app for a while now. I have followed different kinds of tutorial, including the official one by Firebase, adding it both manually and automatic with Firebase Assistant.

For some reason, the following statements are true:

  • onTokenRefresh() in the class extending FirebaseInstanceIdService, is
    NEVER called.
  • onMessageReceived(remoteMessage: RemoteMessage?) in the
    class extending FirebaseInstanceIdService, is NEVER called.
  • No messages are received on

MyFirebaseInstanceIDService:

class MyFirebaseInstanceIDService : FirebaseInstanceIdService() {

    val TAG = javaClass?.simpleName

    override fun onTokenRefresh() {
        super.onTokenRefresh()
        val refreshedToken = FirebaseInstanceId.getInstance().token
        System.out.println("firebasedebug (" + TAG + "): onTokenRefresh(): " + refreshedToken)
    }
}

MyFirebaseMessagingService

class : FirebaseMessagingService() {

    val TAG = javaClass?.simpleName

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        super.onMessageReceived(remoteMessage)
        System.out.println("firebasedebug (" + TAG + "): onMessageReceived(remoteMessage: " + remoteMessage+")")
    }
}

I have added the following to my Manifest:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>

<service
    android:name="dk.minreklame.tilbudsavis.service.firebase.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

<service
    android:name="dk.minreklame.tilbudsavis.service.firebase.MyFirebaseInstanceIDService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

(Note that I have tried setting exported to true and false and also with and without the metatag for setting the channel)

I have also added the following to my Application class:

FirebaseApp.initializeApp(this);

Apparently this was required when I tried to use the method "getToken()" through Firebase. A have also tried with and without these methods.

In my main activity I have tried placing the "getToken()" method through Firebase to attempt to trigger the initialization of the token, but still without any luck.

FirebaseInstanceId.getInstance().token

I have of course added the messaging library to my app module gradle:

compile 'com.google.firebase:firebase-messaging:15.0.0'

And these to my project gradle:

   buildscript {
    ext.kotlin_version = '1.1.4-2'
    ext.anko_version = '0.10.2'
    repositories {
        mavenCentral()
        jcenter()
        google()
    }
    dependencies {
        ...
        classpath 'com.google.gms:google-services:3.2.1' // google-services plugin
        ...
    }
    allprojects {
        tasks.withType(JavaCompile) {
            sourceCompatibility = "1.7"
            targetCompatibility = "1.7"
        }
    }
}

allprojects {
    repositories {
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }
    }
}

When sending the messages through the Firebase dashboard I have tried:

  • Entering the channel
  • Setting target as User segment
  • Setting target as Single device (the token from the app)
  • ...

I have also tried things like:

  • uninstalling and reinstalling the app whenever I tried a new approach
  • deleting instanceid and calling getToken again
  • Trying in both debug and non-debug mode
  • ...

Here is how my list of send messages looks like in the dashboard:
enter image description here

If any more details are needed, please let me know and I will of course supply them (if possible).

Any help will be greatly appreciated.

messaging

Most helpful comment

I have the same issue since FirebaseInstanceIdService gone deprecated, seems you have to use extends FirebaseMessagingService to handle the both things at same service

I have this class

import android.preference.PreferenceManager
import com.dagm8.core.DagM8Constants.USER_DEVICE_TOKEN
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import timber.log.Timber

/**
 * dagm8-android
 * Created by Bedoy on 9/15/17.
 */

class MessagingService : FirebaseMessagingService() {

    private val tag = javaClass.canonicalName

    override fun onNewToken(token: String?) {
        super.onNewToken(token)

        Timber.tag(tag).d("Refreshed token: %s", token)

        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
        val editor = sharedPreferences.edit()

        editor.putString(USER_DEVICE_TOKEN, token)
        editor.apply()
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        super.onMessageReceived(remoteMessage)

        Timber.tag(tag).d("onMessageReceived()")
        Timber.tag(tag).d("Collapse key ${remoteMessage?.collapseKey}")
        Timber.tag(tag).d("From ${remoteMessage?.from}")
        Timber.tag(tag).d("MessageId ${remoteMessage?.messageId}")
        Timber.tag(tag).d("MessageType ${remoteMessage?.messageType}")
        Timber.tag(tag).d("To ${remoteMessage?.to}")
        Timber.tag(tag).d("Data: ")
        remoteMessage?.data?.forEach { key, value ->  Timber.tag(tag).d("$key -> $value")}
    }
}

All 5 comments

class : FirebaseMessagingService() {

val TAG = javaClass?.simpleName

override fun onMessageReceived(remoteMessage: RemoteMessage?) {
    super.onMessageReceived(remoteMessage)
    System.out.println("firebasedebug (" + TAG + "): onMessageReceived(remoteMessage: " + remoteMessage+")")
}

}

try with latest Firebase Messaging service :

implementation 'com.google.firebase:firebase-core:15.0.2'
implementation 'com.google.firebase:firebase-messaging:15.0.2'

I got the same issue, firebase does not work

I forgot all about this issue. But eventually, I figured it out by myself.

What solved it for me, was to remove the following from my manifest:

android:label="@string/app_name"
tools:node="replace"
tools:replace="android:icon,android:label"

If I remember correctly, the above lines were used to override the appearance of the incoming push messages etc. This was originally implemented by a previous developer on my project and it was a coincidence that I discovered this.

@bigant88 Try and see if you have any of the above lines in your manifest. If not, try investigating further to see if you have some other configurations, that overrides anything related to push notifications.
Best of luck to you

I will close this issue now, as my problem described above is solved .

I have the same issue since FirebaseInstanceIdService gone deprecated, seems you have to use extends FirebaseMessagingService to handle the both things at same service

I have this class

import android.preference.PreferenceManager
import com.dagm8.core.DagM8Constants.USER_DEVICE_TOKEN
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import timber.log.Timber

/**
 * dagm8-android
 * Created by Bedoy on 9/15/17.
 */

class MessagingService : FirebaseMessagingService() {

    private val tag = javaClass.canonicalName

    override fun onNewToken(token: String?) {
        super.onNewToken(token)

        Timber.tag(tag).d("Refreshed token: %s", token)

        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
        val editor = sharedPreferences.edit()

        editor.putString(USER_DEVICE_TOKEN, token)
        editor.apply()
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        super.onMessageReceived(remoteMessage)

        Timber.tag(tag).d("onMessageReceived()")
        Timber.tag(tag).d("Collapse key ${remoteMessage?.collapseKey}")
        Timber.tag(tag).d("From ${remoteMessage?.from}")
        Timber.tag(tag).d("MessageId ${remoteMessage?.messageId}")
        Timber.tag(tag).d("MessageType ${remoteMessage?.messageType}")
        Timber.tag(tag).d("To ${remoteMessage?.to}")
        Timber.tag(tag).d("Data: ")
        remoteMessage?.data?.forEach { key, value ->  Timber.tag(tag).d("$key -> $value")}
    }
}
Was this page helpful?
0 / 5 - 0 ratings