5

I already got the push notifications working, but now I have to integrate the in-app notifications. In order to do that I did the following:

Added the lib in the gradle file:

compile "com.facebook.android:notifications:${libs.fb_in_app_notifications}"

Made changes in my main activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Other non related code

    NotificationsManager.presentCardFromNotification(this);
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    setIntent(intent);
    NotificationsManager.presentCardFromNotification(this);
}

And adjusted my already existing FirebaseMessagingService to distinguish the push notifications from the in-app notifications.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static final String PUSH_NOTIFICATION_EXTRA = "push";
private static final String NOTIFICATION_TITLE = "title";
private static final String NOTIFICATION_BODY = "body";

public MyFirebaseMessagingService() {
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Bundle data = new Bundle();
    for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
        data.putString(entry.getKey(), entry.getValue());
    }

    Timber.d("onMessageReceived");
    if (NotificationsManager.canPresentCard(data)) {
        Timber.d("canPresentCard -> in-app notification");
        NotificationsManager.presentNotification(
                this,
                data,
                new Intent(getApplicationContext(), MainActivity.class)
        );
    } else {
        Timber.d("Can not present card -> push notification");
        Context context = this.getApplicationContext();
        Intent defaultAction = new Intent(context, MainActivity.class)
                .setAction(Intent.ACTION_DEFAULT)
                .putExtra(PUSH_NOTIFICATION_EXTRA, data);

        String title = data.getString(NOTIFICATION_TITLE);
        String body = data.getString(NOTIFICATION_BODY);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.launcher)
                .setContentTitle(title == null ? "" : title)
                .setContentText(body == null ? "" : body)
                .setAutoCancel(true)
                .setContentIntent(PendingIntent.getActivity(
                        context,
                        0,
                        defaultAction,
                        PendingIntent.FLAG_UPDATE_CURRENT
                ));

        NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(123, mBuilder.build());
    }
}
} 

The problem is that the method NotificationsManager.canPresentCard() always returns false. Any clue why this could be happening?

Edit 1: The method NotificationsManager.canPresentCard() returns false because the RemoteMessage that it receives does not seem to contain the key fb_push_card for the JSONObject that it expects.

AlvaroSantisteban
  • 5,256
  • 4
  • 41
  • 62

1 Answers1

0

Please Use this below code it worked perfect for me : Add this to build.gradle:

compile 'com.facebook.android:notifications:1.+'

and add this code below:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Bundle data = new Bundle();
    for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
      data.putString(entry.getKey(), entry.getValue());
    }

    NotificationsManager.presentNotification(
        this,
        data,
        new Intent(getApplicationContext(), MainActivity.class)
    );
}

In Your Activity Add this Code :

public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    NotificationsManager.presentCardFromNotification(this);
  }
}

Refer : https://github.com/facebook/FBNotifications

chandrakant sharma
  • 1,334
  • 9
  • 15