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

标签 android push-notification aws-pinpoint

我正在使用 Firebase 通过 Pinpoint 处理推送通知。

当应用程序不在前台时,我能够接收推送通知。但是当应用程序打开时我没有收到它。

我查看了 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 语句(前台应用程序),这会给我正确的结果。但事实证明,如果它在前台,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;
    }

当应用程序处于前台时,事件类型可能是通知未显示的原因。有没有其他方法可以绕过这个?除了提取信息和创建我自己的通知之外?

我找不到任何合适的文档。谁能帮我解决这个问题?

一切都将我引向这个document

最佳答案

没有什么神秘之处——这是设计使然。当应用程序处于前台并且您在 PushListenerService 中的代码改为发送本地广播时,Amazon SDK 不会创建相应的通知。

关于android - 当应用程序在 Android 上处于前台时不显示 Amazon Pinpoint 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55542318/

相关文章:

ios - 在registerUserNotificationSettings之后立即调用registerForRemoteNotifications?

ios - 如何在不使用 parse.com 创建 channel 的情况下向特定设备发送推送通知?

java - AWS Pinpoint - 如何在程序中设置推送通知

android - 为什么我的 AppWidget 没有使用提供的标签?

android - 打开其中之一后,通知中心中的通知消失

android - 自定义 BaseAdapter EditText 单击并在按钮单击中更新数据库

amazon-web-services - 是否可以将 AWS Cognito 托管 UI 与 Amazon Pinpoint 集成以进行分析?

node.js - 使用 AWS Pinpoint 通过 Lambda 函数发送通知

android无法启动模拟器:Cannot run program

android - 无法将项目添加到自定义数组适配器