0

I have followed the FCM docs on setting up the Android app for receiving FCM notifications. My app build.gradle is as follows:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "27.0.1"
defaultConfig {
    applicationId "np.com.ravi.fcmtest"
    minSdkVersion 15
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'

testCompile 'junit:junit:4.12'
//for firebase notifications
compile 'com.google.firebase:firebase-messaging:10.0.1'
}

//for firebase notifications
apply plugin: 'com.google.gms.google-services'

And my project level build.gradle is

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    //for firebase notifications
    classpath 'com.google.gms:google-services:3.0.0'
}
}

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

task clean(type: Delete) {
delete rootProject.buildDir
}

I have a MainActivity that has basically no code

I have two services as:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";

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

    // TODO: Implement this method to send any registration to your app's servers.
    sendRegistrationToServer(refreshedToken);
}

/**
 * Persist token to third-party servers.
 *
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.
}
}

And

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";

@Override
public void onDeletedMessages() {
    Log.d(TAG, "Notification message deleted");
}

@Override
public void onSendError(String s, Exception e) {
    Log.d(TAG, s);
}

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

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO: Handle FCM messages here.
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

    //createNotification(remoteMessage.getNotification().getBody());
    // Create Notification
    Log.d("On", "createNotification");

    //for when the notification is tapped we are directed to the MainActivity
    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);

    //setting notification attributes
    Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    String CHANNEL_ID = "my_channel_01";
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle("FCM Test")
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);

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

    //post notification to notification bar
    notificationManager.notify(0, notificationBuilder.build());

}
}

I have also registered the two services in my AndroidManifest.xml

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

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

I should be getting the firebase notifications. But I am not receiving any messages in my LogCat. I am also not getting them in my status bar as notifications regardless of whether my app is in the foreground or the background.

The messages I am receiving in my Logcat are:

12-14 12:40:46.140 32287-32287/np.com.ravi.fcmtest D/AppTracker: App Event: stop
12-14 12:40:46.218 32287-2094/np.com.ravi.fcmtest V/FA: Recording user engagement, ms: 78587
12-14 12:40:46.220 32287-2094/np.com.ravi.fcmtest V/FA: Using measurement service
12-14 12:40:46.220 32287-2094/np.com.ravi.fcmtest V/FA: Connecting to remote service
12-14 12:40:46.224 32287-2094/np.com.ravi.fcmtest V/FA: Activity paused, time: 949735552
12-14 12:40:46.234 32287-2094/np.com.ravi.fcmtest I/FA: Tag Manager is not found and thus will not be used
12-14 12:40:46.240 32287-2094/np.com.ravi.fcmtest D/FA: Logging event (FE): _e, Bundle[{_o=auto, _et=78587, _sc=MainActivity, _si=-8670171141235923720}]
12-14 12:40:46.269 32287-2094/np.com.ravi.fcmtest V/FA: Using measurement service
12-14 12:40:46.269 32287-2094/np.com.ravi.fcmtest V/FA: Connection attempt already in progress
12-14 12:40:46.270 32287-2094/np.com.ravi.fcmtest D/FA: Connected to remote service
12-14 12:40:46.270 32287-2094/np.com.ravi.fcmtest V/FA: Processing queued up service tasks: 2
12-14 12:40:51.313 32287-2094/np.com.ravi.fcmtest V/FA: Inactivity, disconnecting from the service

Please help me get through this. Thanks.

AL.
  • 36,815
  • 10
  • 142
  • 281
ravi
  • 899
  • 8
  • 31
  • Did you debug the code ? Did onMessageReceived called for each notification? – ADM Dec 14 '17 at 07:12
  • Which device are you testing on? – Aswin P Ashok Dec 14 '17 at 07:14
  • @ravi please check my ans and update your gradle. – Ratilal Chopda Dec 14 '17 at 07:17
  • @AswinPAshok I am testing on OnePlus 3T – ravi Dec 14 '17 at 07:36
  • @Guruji I added the dependency for firebase core and still no luck. I am using Firebase console to send messages BTW. The console says that the messages are sent, but I have no luck catching them on the Log. – ravi Dec 14 '17 at 07:39
  • do you get notification when app is in foreground? – Aswin P Ashok Dec 14 '17 at 07:43
  • @Guruji I received the message in the log. Thanks. But the notification is not being generated. Do you know why? – ravi Dec 14 '17 at 07:43
  • @AswinPAshok I added the firebase core dependency, the fcm message did show up on my log while the app was in foreground. But not in the status bar as notification. – ravi Dec 14 '17 at 07:45
  • try this. In Settings Menu go to Battery settings and disabled Aggressive doze & App Hibernation. I got it from a one plus forum. Worth a shot – Aswin P Ashok Dec 14 '17 at 07:46
  • @AswinPAshok There is no such thing in my Battery Settings. https://postimg.org/image/xee1cowxd/ – ravi Dec 14 '17 at 07:52
  • Do you have notification issues on other apps? Which OS are you on? – Aswin P Ashok Dec 14 '17 at 07:53
  • @AswinPAshok Nope, none at all. In fact I have another app that I wrote that generates different types of Notifications (inbox style, picture style and so on) on Button click which works fine. – ravi Dec 14 '17 at 07:56
  • your os? Factory OS or custom ROM? – Aswin P Ashok Dec 14 '17 at 07:57
  • @Guruji I am getting `Processing queued up service tasks: 2` in my LogCat. What does that mean? – ravi Dec 14 '17 at 08:00
  • @AswinPAshok Oxygen OS, Android Oreo. Factory OS. – ravi Dec 14 '17 at 08:00
  • Try that in some other phone, or an emulator. OP3T seems to have some [issues with push notifications](https://forums.oneplus.net/threads/no-notifications.478745/) – Aswin P Ashok Dec 14 '17 at 08:05
  • Take a look at [this question](https://stackoverflow.com/questions/43093260/notification-not-showing-in-android-8-oreo). It may help – Aswin P Ashok Dec 14 '17 at 08:28
  • @AswinPAshok Thank you very much. I tested it on my friend's phone and it worked. I am disappointed with my phone for the first time. Can you suggest any workaround with this problem? – ravi Dec 14 '17 at 08:33
  • @ravi Happy to help you.Happy coding. – Ratilal Chopda Dec 14 '17 at 08:39
  • If it is not an oxygenos bug, there should be a settings somewhere to fix it. Check the Battery Optimization settings under battery settings. Look for aggressive doze, autostart manager or something like that. **Test it on some other OREO device.** If it is not working on any oreo device, check this https://stackoverflow.com/questions/43093260/notification-not-showing-in-android-8-oreo From your code, it seems you have'nt used [NotificationChannel](https://developer.android.com/reference/android/app/NotificationChannel.html) which is essential to show notifications in oreo.. – Aswin P Ashok Dec 14 '17 at 09:16
  • Go here : https://developer.android.com/about/versions/oreo/android-8.0.html And read what changed in notification API – Aswin P Ashok Dec 14 '17 at 09:25

2 Answers2

0

you can add firebase-core must be to get firebase notification and message. in your gradle

compile 'com.google.firebase:firebase-core:10.0.1'    
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
  • @ravi Processing queued up service tasks: 2 you can try restart your device or check in other device or other phone, or an emulator. – Ratilal Chopda Dec 14 '17 at 08:36
-1
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.e(TAG, "From: " + remoteMessage.getData().toString());
}

Write this line and check notification data is getting

Neil
  • 93
  • 3
  • 8