2

UPDATE

Unfortunately, I didn't solve this issue and what I did is create a new project. Fortunately my new project works. One thing I noticed from my newly created project, when I am sending notification messages, some messages didn't arrive to my device. So I think its my Internet connection problem (my idea only).

I am trying to implement basic receiving of Push Notifications using Firebase. I based this tutorial:

https://www.codementor.io/android/tutorial/send-push-notifications-to-android-with-firebase

My Problem is I don't receive any messages at all. I have sent more than 5 messages using the Firebase Console and still nothing happens.

Here is my implementation of FirebaseInstanceIdService:

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

public static final String debugTag = "MyFirebaseIIDService";

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();

    //Getting registration token
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();

    //Displaying token in logcat
    Log.e(debugTag, "Refreshed token: " + refreshedToken);

}

private void sendRegistrationToServer(String token) {
    //You can implement this method to store the token on your server
    //Not required for current project
}

}

My FirebaseMessagingService implementation:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static final String debugTag = "MyFirebaseApp";

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

    Log.d(debugTag, "Notification Received!");

    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

}

//This method is only generating push notification
private void sendNotification(String title, 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.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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

}

My Manifest file:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".activities.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <service
        android:name=".messagingservice.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

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

</application>

What am I doing wrong? Or what am I lacking to implement?

I am really needing some help with this one. Any help will be greatly appreciated. Thank you very much!

Chris Palma
  • 223
  • 1
  • 2
  • 13
  • 1
    Did you receive the notifications in the mobile device's system tray? or just the `onMessageReceived` method is not being called? – GeorgeLBA Aug 24 '16 at 10:46
  • 1
    How did you tried ?I mean when the app is in Foreground or Background ?. – Sunil Sunny Aug 24 '16 at 10:46
  • 1
    @Chris Palma did you included the google-services.json file in your module level directory.? – Swaminathan V Aug 24 '16 at 14:09
  • Yes I included the json file in my directory. I also tried my app to be foreground and background. I really didn't receive any notifications. – Chris Palma Aug 24 '16 at 23:43
  • 1
    @ChrisPalma In your code you have the following line in the onMessageReceived method `Log.d(debugTag, "Notification Received!");` is it printing the message in the logcat? Did you receive at least the notification in the mobile device's system tray? – GeorgeLBA Aug 25 '16 at 07:06
  • I didn't really get anything AT ALL, sir GeorgeLBA. What I have done is that I created a new project and fortunately it works. I really don't have an idea about why it fails (my first project). – Chris Palma Aug 25 '16 at 08:39
  • try to regenerate google-services.json, you may have a bad configuration file in your app and because of that, firebase can't pair your app with firebase backend – mayosk Aug 25 '16 at 08:49
  • onRefreshToken() deprecated, [check here](https://stackoverflow.com/questions/51951864/fcm-404-requested-entity-was-not-found-errorcode-unregistered) – DV Singh Aug 28 '20 at 03:40

2 Answers2

1

In my case, every thing was OK,but the app's notification channels was inactive. so make sure your notification channel is active in setting.

may help some one.

Gg M
  • 386
  • 3
  • 6
0

onRefreshToken() deprecated, Use onNewToken(String s)

check here

DV Singh
  • 1,038
  • 11
  • 16