简体   繁体   English

当应用程序在 Android 上处于前台时不显示 Amazon Pinpoint 推送通知

[英]Amazon Pinpoint Push Notification not displayed when the app is in foreground on Android

I am working on Push notifications through Pinpoint using Firebase.我正在使用 Firebase 通过 Pinpoint 处理推送通知。

I am able to receive push notifications when the app is not in foreground.当应用程序不在前台时,我能够收到推送通知。 However I don't receive it when the app is open.但是当应用程序打开时我没有收到它。

I looked at the PushListenerService code and it had something like this:我查看了 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;
    }

I thought of playing around the if statement (App in Foreground one)and that will give me proper results.我想过使用 if 语句(前台应用程序之一),这会给我正确的结果。 But it turns out that the sdk has the logic inside to not to show notification if it is in foreground.但事实证明,sdk 内部有逻辑,如果它在前台,则不显示通知。

Here's a little snippet from the 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;
    }

Probably the eventType is the cause why notifications are not showing when the app is in foreground.可能 eventType 是应用程序处于前台时未显示通知的原因。 Is there any alternate ways to bypass this?有没有其他方法可以绕过这个? Other than extracting information and creating my own Notification?除了提取信息和创建我自己的通知?

I can't find any proper documentation for this.我找不到任何适当的文档。 Can anyone help me out on this?任何人都可以帮我解决这个问题吗?

Everything just leads me to this document一切都让我找到了这份 文件

There is no mystery - this is by design.没有什么神秘之处 - 这是设计使然。 Amazon SDK doesn't create corresponding notification when app is in foreground and your code in PushListenerService sends local broadcast instead.当应用程序处于前台并且您在 PushListenerService 中的代码发送本地广播时,Amazon SDK 不会创建相应的通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将推送通知的已读回执发送到Android上的Amazon Pinpoint? - How to send a read receipt of a push notification to Amazon Pinpoint on Android? 在Android中-当应用程序处于前台时如何防止Firebase推送通知? - In android - How to prevent Firebase push notification when app is in foreground? Amazon Pinpoint 和 Ionic - 当应用程序在后台时推送通知不起作用 - Amazon Pinpoint and Ionic - Push notifications not working when app is in background 当应用程序处于前台时发送推送通知 - Push notification is sent when app is in foreground Android 仅当应用程序处于前台时才播放推送通知声音,但当应用程序处于后台时不播放声音 - Android Push notification sound is played only when app is in foreground but not playing sound when app is in background 当应用程序不在后台或前台时,无法将通知推送到通知管理器 - When the app is not in background or foreground, not able push Notification to Notification manager 前台通知 android 未显示(奥利奥) - Notification android on foreground not displayed (Oreo) Android-在前台运行应用程序时处理本地通知 - Android - Handle local notification when app in foreground 如果应用程序在前台,通知不会显示在通知抽屉中 - Notification is not displayed in notification drawer if app is in foreground 推送通知未显示在 Android 前台 - Push notification not showing in Android foreground
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM