Amazon Pinpoint Push Notification не отображается, когда приложение находится на переднем плане на Android - PullRequest
2 голосов
/ 05 апреля 2019

Я работаю над Push-уведомлениями через Pinpoint, используя Firebase.

Я могу получать push-уведомления, когда приложение не на переднем плане. Однако я не получаю его, когда приложение открыто.

Я посмотрел на код PushListenerService, и он выглядел примерно так:

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d(TAG, "Message: " + remoteMessage.getData());

    final NotificationClient notificationClient= NofiticaiontProvider.getNotificationClient();

    final NotificationDetails notificationDetails = NotificationDetails.builder()
            .from(remoteMessage.getFrom())
            .mapData(remoteMessage.getData())
            .intentAction(NotificationClient.FCM_INTENT_ACTION)
            .build();

    NotificationClient.CampaignPushResult pushResult = notificationClient.handleCampaignPush(notificationDetails);

    if (!NotificationClient.CampaignPushResult.NOT_HANDLED.equals(pushResult)) {
        /**
         The push message was due to a Pinpoint campaign.
         If the app was in the background, a local notification was added
         in the notification center. If the app was in the foreground, an
         event was recorded indicating the app was in the foreground,
         for the demo, we will broadcast the notification to let the main
         activity display it in a dialog.
         */
        if (NotificationClient.CampaignPushResult.APP_IN_FOREGROUND.equals(pushResult)) {
            /* Create a message that will display the raw data of the campaign push in a dialog. */

            final HashMap<String, String> dataMap = new HashMap<>(remoteMessage.getData());
            broadcast(remoteMessage.getFrom(), dataMap);
        }
        return;
    }

Я подумал об игре с оператором if (приложение в Foreground one), и это даст мне надлежащие результаты. Но оказывается, что внутри SDK есть логика, чтобы не показывать уведомление, если оно находится на переднем плане.

Вот небольшой фрагмент из SDK

public final NotificationClient.CampaignPushResult handleCampaignPush(NotificationDetails notificationDetails) {
    final String from = notificationDetails.getFrom();
    final Bundle data = notificationDetails.getBundle();
    final Class<?> targetClass = notificationDetails.getTargetClass();
    String intentAction = notificationDetails.getIntentAction();
    notificationChannelId = notificationDetails.getNotificationChannelId();

    // Check if push data contains a Campaign Id
    if (data == null || !data.containsKey(CAMPAIGN_ID_PUSH_KEY)) {
        return NotificationClient.CampaignPushResult.NOT_HANDLED;
    }

    final boolean isAppInForeground = appUtil.isAppInForeground();

    final String imageUrl = data.getString(CAMPAIGN_IMAGE_PUSH_KEY);
    final String imageIconUrl = data.getString(CAMPAIGN_IMAGE_ICON_PUSH_KEY);
    final String imageSmallIconUrl = data.getString(CAMPAIGN_IMAGE_SMALL_ICON_PUSH_KEY);
    final Map<String, String> campaignAttributes = new HashMap<String, String>();

    campaignAttributes.put(CAMPAIGN_ID_ATTRIBUTE_KEY, data.getString(CAMPAIGN_ID_PUSH_KEY));
    campaignAttributes.put(CAMPAIGN_TREATMENT_ID_ATTRIBUTE_KEY, data.getString(CAMPAIGN_TREATMENT_ID_PUSH_KEY));
    campaignAttributes.put(CAMPAIGN_ACTIVITY_ID_ATTRIBUTE_KEY, data.getString(CAMPAIGN_ACTIVITY_ID_PUSH_KEY));

    this.pinpointContext.getAnalyticsClient().setCampaignAttributes(campaignAttributes);
    log.info("Campaign Attributes are:" + campaignAttributes);

    if (AWS_EVENT_TYPE_OPENED.equals(from)) {
        return this.handleNotificationOpen(campaignAttributes, data);
    }

    // Create the push event.
    String eventType = null;
    if (isAppInForeground) {
        eventType = AWS_EVENT_TYPE_RECEIVED_FOREGROUND;
    } else {
        eventType = AWS_EVENT_TYPE_RECEIVED_BACKGROUND;
    }

Возможно, eventType является причиной, по которой уведомления не отображаются, когда приложение находится на переднем плане. Есть ли альтернативные способы обойти это? Кроме извлечения информации и создания моего собственного уведомления?

Я не могу найти соответствующую документацию для этого. Кто-нибудь может мне помочь в этом?

Все просто приводит меня к этому документу

...