8

I am trying to use FireBase Cloud Messaging. I am receiving a token but its not getting any notifications from console. Here is my Manifest:

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="careerage.jobseeker.app"
android:hardwareAccelerated="true"
android:versionCode="1"
android:versionName="0.0.1">

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="23" />

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:resizeable="true"
    android:smallScreens="true"
    android:xlargeScreens="true" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:hardwareAccelerated="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:supportsRtl="true">
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
        android:label="@string/activity_name"
        android:launchMode="singleTop"
        android:theme="@android:style/Theme.DeviceDefault.NoActionBar"
        android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/launcher_name">
            <action android:name="android.intent.action.MAIN" />

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

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

And My Code is almost the same as from the sample:

public class NotificationService extends FirebaseMessagingService {

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 , intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("Notified")
            .setContentText(messagebody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0 , notificationBuilder.build());
}

private void sendSnackbar(String messagebody)
{
    Toast.makeText(this,"Notified",Toast.LENGTH_LONG).show();
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Toast.makeText(this,"Message Received",Toast.LENGTH_LONG).show();

        Log.d("Notification Received",remoteMessage.getNotification().getBody());

        sendNotification(remoteMessage.getNotification().getBody());
    sendSnackbar(remoteMessage.getNotification().getBody());
}

}

I checked the Question here Firebase cloud messaging notification not received by device but still it is not working.

Community
  • 1
  • 1
Yash Jain
  • 376
  • 1
  • 3
  • 13
  • Are you sending the payload via the console? – AL. May 27 '16 at 07:48
  • @McAwesomville Yep and its working with their sample – Yash Jain May 27 '16 at 07:49
  • Sorry. I didn't see earlier, you already mentioned that. Anyways, so the logs in your `onMessageReceived()` did not show up after you sent the Notification? Was the app in foreground or background? – AL. May 27 '16 at 07:51
  • The logs didn't show up and the app was running in foreground – Yash Jain May 27 '16 at 07:55
  • That's odd. Are you sure you're specifying the correct `registration token`? – AL. May 27 '16 at 07:56
  • I am sending it to everyone, Its status is completed. I tried with the registration token, but the status of the notification is coming FAILED – Yash Jain May 27 '16 at 07:59
  • Is there any error at the logcat at all? – AL. May 27 '16 at 08:10
  • Nope no error in logcat – Yash Jain May 27 '16 at 08:11
  • If the message fails when it's sent to the specific token it mostly likely means that the token is invalid / not correctly associated with your app. Can you double check how you obtained that token? also check that you added the right package name to the firebase console and you downloaded the google-services.json file. Finally please try to uninstall and re install the app and get a new token. – Diego Giorgini May 27 '16 at 18:27
  • @DiegoGiorgini I did try reinstalling and installing the app. And also checked the google-services.json file. I think its correct. But how do I double check how the token was obtained – Yash Jain May 28 '16 at 06:19

2 Answers2

1

Try sending message using service api call. It will provide you the response code and response json which may help you to get the reason for not working FCM.

Service call usage answered here- Firebase onMessageReceived not called when app in background

Community
  • 1
  • 1
Ankit Adlakha
  • 1,436
  • 12
  • 15
-2

You need to change the service in with the whole package name like below,

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

Please let me know if it is not working.

Ramesh
  • 47
  • 5