gpt4 book ai didi

android - 在 onMessageReceived 中显示警报对话框 - FCM

转载 作者:行者123 更新时间:2023-11-29 01:03:59 25 4
gpt4 key购买 nike

我试图在 MyFirebaseMessagingService 类的 onMessageReceived 中显示警告消息,但我收到错误消息:

    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.app.Activity.<init>(Activity.java:846)
at android.support.v4.app.SupportActivity.<init>(SupportActivity.java:38)
at android.support.v4.app.BaseFragmentActivityApi14.<init>(BaseFragmentActivityApi14.java:28)
at android.support.v4.app.BaseFragmentActivityApi16.<init>(BaseFragmentActivityApi16.java:34)
at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:67)
at android.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:61)
at com.dopay.onboarding.activity.BaseActivity.<init>(BaseActivity.java:51)
at com.dopay.onboarding.FMS.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:75)
at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source)
at com.google.firebase.iid.zzc.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
02

我尝试过的。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

if (remoteMessage.getData().size() > 0) {
BaseActivity baseActivity = new BaseActivity();
baseActivity.showDialog();
}

}

基础 Activity

  public void showDialog(){
this.runOnUiThread(new Runnable() {
public void run() {
//Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
DialogUtil.showAlert(getApplicationContext(), R.string.message_complete_required_fields);
}
});
}

DialogUtil

  public static void showAlert(Context context, int messageId) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(messageId)
.setCancelable(true)
.setPositiveButton(context.getResources().getString(R.string.label_ok), null);
builder.create().show();
}

有关如何在 onMessageReceived 中打开警告对话框的任何建议。

谢谢R

最佳答案

这是我为您的问题支付的两分钱:

  1. 使用 Activity 上下文来显示对话框或与此相关的任何 UI 相关内容。还有你开始 Activity 的方式,而不是它的打开方式。您可以选择在顶部栏上显示通知并在其上设置 PendingIntent,以便单击它打开所需的 Activity。 Open specific Activity when notification clicked in FCM

  2. 与其从 onMessageReceived 获取对 BaseActivity 的引用,不如让它们分离。使用 LocalBroadcastManagerHandler 向 Activity 发送消息以显示对话框。这样,如果 Activity 处于打开状态,它将对其进行操作,否则它不会执行任何操作。检查GCM IntentService how to display a pop up on notification receive

关于android - 在 onMessageReceived 中显示警报对话框 - FCM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48939133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com