Facebook应用内通知无效

时间:2018-01-13 18:35:06

标签: android facebook

这是我对应用内通知的集成

FCM服务

public class MyFirebaseMessagingService extends FirebaseMessagingService {


private static final String NOTIFICATION_CHANNEL_ID = "GrowFit";


@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());
    }

    Context context = this.getApplicationContext();
    Intent defaultAction = new Intent(context, Dashboard.class)
            .setAction(Intent.ACTION_DEFAULT)
            .putExtra("push", data);

    String title = data.getString("title");
    String body = data.getString("body");

    Vars.bundle = data;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Grow Fit", NotificationManager.IMPORTANCE_HIGH);

        notificationChannel.setDescription("GrowFit Notifications");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.BLUE);
        notificationChannel.enableVibration(true);
        notificationChannel.canShowBadge();
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_stat_growfit)
            .setContentTitle(title == null ? "Hey" : title)
            .setContentText(body == null ? "Having a good day?" : body)
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(
                    context,
                    0,
                    defaultAction,
                    PendingIntent.FLAG_UPDATE_CURRENT
            ));

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (mNotificationManager != null) {
        mNotificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), mBuilder.build());
    }



NotificationsManager.presentNotification(this, data, new Intent(getApplicationContext(), Dashboard.class), new NotificationsManager.NotificationExtender() {
                @Override
                public Notification.Builder extendNotification(@NonNull Notification.Builder builder) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
                    }
                    return mBuilder;
                }
            });

}

Single Top Activity 处理意图)

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    checkInAppPush(intent);
    checkFBPush();
}

private void checkInAppPush(Intent intent) {
    NotificationsManager.presentCardFromNotification(this, intent);
}

意图有额外内容但是没有呈现应用内通知。我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:0)

您不需要使用Android Notification Manager来显示Facebook应用内通知,使用fb In-app push noti&gt;&gt;您必须要整合fb通知库&gt;&gt; https://github.com/facebook/FBNotifications 集成fb通知后,需要在onMessageReceived方法中编写NotificationManager.canPresentCard(data)

 Bundle data = new Bundle();
    for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
        data.putString(entry.getKey(), entry.getValue());
    }
    Log.d(TAG, "onMessageReceived: "+ data.toString());

  if (NotificationsManager.canPresentCard(data)) {
        Log.d(TAG, "onMessageReceived: " + "PresentCard");
        NotificationsManager.presentNotification(
                this,
                data,
                new Intent(getApplicationContext(), MainActivity.class)
                //MainActivity.getIntent(getApplicationContext(),data)
        );
   }

here is my In-app noti