【问题标题】:Display Message callback not invoking - Firebase in-app messaging显示消息回调未调用 - Firebase 应用内消息
【发布时间】:2019-02-28 13:01:46
【问题描述】:

我正在尝试自定义 Firebase 应用内消息。

我正在关注这篇文章:https://firebase.google.com/docs/in-app-messaging/customize-messages

根据文章,我创建了自己的 FirebaseInAppMessagingDisplay 类实现。

import com.google.firebase.inappmessaging.FirebaseInAppMessagingDisplay;
import com.google.firebase.inappmessaging.FirebaseInAppMessagingDisplayCallbacks;
import com.google.firebase.inappmessaging.model.InAppMessage;

public class MyMessageDisplayImplementation implements 
FirebaseInAppMessagingDisplay {
    @Override
    public void displayMessage(InAppMessage inAppMessage
        , FirebaseInAppMessagingDisplayCallbacks 
    firebaseInAppMessagingDisplayCallbacks) {
         Log.e("INAPP_MESSAGE","received an inapp message");
    }
}

然后使用无头 Firebase 应用内消息 SDK 注册此实现

public class MyApplication extends Application{

@Override
public void onCreate() {
    super.onCreate();
    FirebaseInAppMessaging.getInstance().setMessageDisplayComponent(new MyMessageDisplayImplementation());
}

}

我的问题是,我没有收到 displyaMessage() 回调。

当我从 Application 类中注释掉代码行 " FirebaseInAppMessaging.getInstance().setMessageDisplayComponent(new MyMessageDisplayImplementation());" 时,它显示的是默认消息。但是,当我把这段代码放回去时,什么都没有发生。

如果有人对此应用内消息自定义有更好的了解,请提供帮助。

【问题讨论】:

  • 嘿,兄弟,你找到解决办法了吗,请发帖

标签: android firebase android-custom-view firebase-in-app-messaging


【解决方案1】:

firebase 文档上的信息有点混乱。其实很简单。

在应用级 gradle 文件中添加了这些依赖项。

implementation 'com.google.firebase:firebase-core:16.0.8'
implementation ("com.google.firebase:firebase-inappmessaging:17.0.3")

注意:我们不需要添加“implementation 'com.google.firebase:firebase-inappmessaging-display:17.1.1'”依赖

在启动活动时注册 DisplayMessage 组件。

import com.google.firebase.inappmessaging.FirebaseInAppMessaging
import com.google.firebase.inappmessaging.FirebaseInAppMessagingDisplay

///////

override fun onStart() {
    super.onStart()
    Log.e("MESSAGE", "activity started")
    var firebaseInAppMessagingDisplay = FirebaseInAppMessagingDisplay { inAppMessage, cb ->
        // You can show the message here.
        // The variable inAppMessage has all information about the campaign that we putting in console (title, content, image url.. etc)
        Log.e("MESSAGE", "Display Message callback invoked")
    }
    FirebaseInAppMessaging.getInstance().setMessageDisplayComponent(firebaseInAppMessagingDisplay)
}

【讨论】:

  • 你能帮帮我吗?当我实现回调时,永远不会调用
【解决方案2】:

This GitHub issue comment 为我解决了这个问题。

简而言之,问题是由于 Firebase SDK 在您的活动恢复时设置了自己的侦听器,从而覆盖了您使用 setMessageDisplayComponent 设置的侦听器。

为了解决这个问题,我将自己的侦听器代码从 onStart 移至 onResume,确保我的侦听器是最后添加的并成为接收消息的那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2021-12-27
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    相关资源
    最近更新 更多