4

I've looked over all the answers I could find, the particular function of code isn't working even in foreground. I tried changing the manifest, changing the code, all i get in the logger are these 2 of these things: D/FA: Logging event (FE): notification_receive(_nr), ...

This is my manifest file:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@drawable/avatar"
    android:label="@string/app_name"
    android:roundIcon="@drawable/avatar"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <activity android:name=".StartActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"/>

    <service android:name=".GettingDeviceTokenService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <service android:name=".NotificationService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

</application>

And here is the notification service:

public class NotificationService extends FirebaseMessagingService {

public static  int NOTIFICATION_ID = 1;
private static final String CHANNEL_ID = "1";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d("+++", remoteMessage.toString());
    if (remoteMessage.getData().size() > 0) {
        Log.d("dataa", "Data Payload: " + remoteMessage.getData().toString());
    }

}

This is the app gradle file

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.xghos.Wrenchy"
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'net.hockeyapp.android:HockeySDK:5.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-messaging:17.3.0'
implementation 'com.ethanhua:skeleton:1.1.1'
implementation 'io.supercharge:shimmerlayout:2.1.0'
implementation "com.daimajia.swipelayout:library:1.2.0@aar"
}

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
        if (!requested.name.startsWith("multidex")) {
            details.useVersion '26.1.0'
        }
    }
}
}

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

and the GettingDeviceTokenService:

package com.example.xghos.Wrenchy;

import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class GettingDeviceTokenService extends FirebaseMessagingService {

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.d("DeviceToken ==> ",  s);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
}

}

EDIT: the problem was solved by changing the intent filter of the GettingDeviceTokenService from

<action android:name="com.google.firebase.MESSAGING_EVENT" />

to

<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
AL.
  • 36,815
  • 10
  • 142
  • 281
  • 1
    check this answer https://stackoverflow.com/a/40458361/6559031 – Devil10 Aug 17 '18 at 04:12
  • It's irrelevant for now, i'm not yet trying to get either data or notification payloads, i'm just trying to log the remote message and it doesn't log it. The app is in foreground and i don't get why it's not working at all, it should call onMessageReceived if app is in foreground for the notification as well as the data payload – Andrew Daniel Barzu Aug 17 '18 at 07:23
  • post your gradle file.. and whats your `GettingTokenService()`. because you already declare both `GettingTokenService` and `Notification Service` as MESSAGE_EVENTS – Devil10 Aug 17 '18 at 07:27
  • 1
    Your `GettingTokenService()` must be having intent filter `` – Devil10 Aug 17 '18 at 07:33
  • Changed it right now, checking if anything works now – Andrew Daniel Barzu Aug 17 '18 at 07:37
  • 1
    It worked finally, the problem really was that i declared 2 services with the intent filter of MESSAGING_EVENT, thanks everyone for the help! – Andrew Daniel Barzu Aug 17 '18 at 07:42

3 Answers3

1

You should consider difference between data message and display message.

Display message has payload with key notification and are automatically displayed when app is in background and also call onMessageReceived() if app is already in foreground.

Data message has payload key data . They always invoke onMessageReceived()

Shashanth
  • 4,995
  • 7
  • 41
  • 51
mnp343
  • 329
  • 1
  • 6
  • I'm trying to make it work in foreground for now at least, and those two logs aren't even showing, idk if it's anything about the notification/data key payloads from the notification, i can't even see the logs that i give in the onMessageReceived, so the function isn't even called when a message is received – Andrew Daniel Barzu Aug 17 '18 at 07:19
  • Function will be call for sure if you send data message from firebase irrespective of app is in foreground or background – mnp343 Aug 17 '18 at 08:37
1

You have registered NotificationService service in your manifest. Since latest update for firebase notifications, the same service will handle new token and received messages. In your case you should register in manifest only GettingDeviceTokenService because you override both methods and remove NotificationService.

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
0

Try this way. It worked for me Perfectly. 1. Create a Service

public class YourService extends FirebaseMessagingService {

public static int NOTIFICATION_ID = 1;

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this, "2")
            .setSmallIcon(R.drawable.your_icon)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (NOTIFICATION_ID > 1073741824) {
        NOTIFICATION_ID = 0;
    }
    Objects.requireNonNull(notificationManager).notify(NOTIFICATION_ID++, mNotifyBuilder.build());
}

}

Now add this to Manifest

<service
   android:name=".YourService"
   android:exported="false">
   <intent-filter>
       <action android:name="com.google.firebase.MESSAGING_EVENT" />
   </intent-filter>
</service>
Yasiru Nayanajith
  • 1,647
  • 17
  • 20
  • I have separate functions for handling notifications, but i'm not using them yet because i can't even get the 2 logs that i'm showing there to work. Also, i have it declared in the manifest as you can see. – Andrew Daniel Barzu Aug 17 '18 at 07:26