Facebook Analytics - 应用内通知不起作用

Alv*_*ban 5 android facebook-analytics facebook-analytics-push

我已经有推送通知工作,但现在我必须集成应用内通知.为此,我做了以下事情:

在gradle文件中添加了lib:

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

改变了我的主要活动:

@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);
}
Run Code Online (Sandbox Code Playgroud)

并调整我已经存在的FirebaseMessagingService以区分推送通知和应用内通知.

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());
    }
}
} 
Run Code Online (Sandbox Code Playgroud)

问题是该方法NotificationsManager.canPresentCard()总是返回false.任何线索为什么会发生这种情况?

编辑1:该方法NotificationsManager.canPresentCard()返回false,因为RemoteMessage它接收到的内容似乎不包含fb_push_card它所期望的JSONObject 的键.

cha*_*rma 0

请使用下面的代码,它对我来说非常有效:将其添加到build.gradle

compile 'com.facebook.android:notifications:1.+'
Run Code Online (Sandbox Code Playgroud)

并添加以下代码:

@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)
    );
}
Run Code Online (Sandbox Code Playgroud)

在您的活动中添加以下代码:

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

参考: https: //github.com/facebook/FBNotifications