Facebook Analytics - notification in the application does not work

I already have push notifications, but now I need to integrate the notifications in the application . To do this, I did the following:

Added lib to 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 the existing FirebaseMessagingService to distinguish push notifications from notifications in the application.

 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 NotificationsManager.canPresentCard() method always returns false. Any clue why this might happen?

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

+5
source share
1 answer

Please use this code below to make it work perfectly for me: Add this to build.gradle :

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

and add the following code:

 @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

0
source

Source: https://habr.com/ru/post/1272721/


All Articles