Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marshmallow (API Level 23) Firebase messaging is not working

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.myapp.service.firebase.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

<service
    android:name="dk.myapp.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 (and google play service modules) to my app module gradle:

compile 'com.google.firebase:firebase-messaging:15.0.0'
compile 'com.google.android.gms:play-services-ads:15.0.0'
compile 'com.google.android.gms:play-services-maps:15.0.0'
compile 'com.google.android.gms:play-services-base:15.0.0'
compile 'com.google.android.gms:play-services-location:15.0.0'

An also added this line to the bottom of the gradle file:

apply plugin: 'com.google.gms.google-services'

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
        }
    }
}

EDIT: I have of course also added the google-services.json to the project

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).

EDIT I tried out the REST API and made some observations.

  • First time I tried sending a standard notification I got a response with success: 1 and failure: 0, but the other way around afterward (failure: 1) with "error": "NotRegistered" (see the image below)

enter image description here

  • I get a 200 back every time.
  • If I uninstall/reinstall the app and use the new token, the same things as described above, happends again.
  • None of of the attempts gave me a visible push message on the device, nor did onMessageReceived method get called, which I have tested through log messages and syso's in the code and with the debugger.
like image 220
Langkiller Avatar asked May 04 '18 11:05

Langkiller


1 Answers

I have finally found the solution to my particular situation.

The solution for me was to remove the following lines from my manifest file in the application tag:

android:label="@string/app_name"
tools:node="replace"
tools:replace="android:icon,android:label"
like image 159
Langkiller Avatar answered Sep 30 '22 18:09

Langkiller