单击推送通知后,当应用程序进入前台时如何显示自定义对话框

Posted

技术标签:

【中文标题】单击推送通知后,当应用程序进入前台时如何显示自定义对话框【英文标题】:How to display custom dialog box when application comes to foreground after clicking on Push notification 【发布时间】:2017-09-11 17:44:16 【问题描述】:

我有一个自定义对话框,希望在用户单击推送通知后显示。我在这里使用FirebaseMessagingService

以下是我接收推送通知的代码。我正在创建一个意图并广播它。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) 
    super.onMessageReceived(remoteMessage);

   if (remoteMessage.getData().size() > 0) 
        Intent intent = new Intent(Cons.INTENT_FILTER);

        try 
            // getting my data here
            Bundle bundle = new Bundle();
            bundle.putSerializable(key, data);
            intent.putExtras(bundle);
        
        catch (Exception e)

        
        sendBroadcast(intent);
    


在我的基本活动中,我使用BroadcastReceiver 接收意图并显示对话框。

private BroadcastReceiver myReceiver = new BroadcastReceiver() 
    @Override
    public void onReceive(Context context, Intent intent) 

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        DialogFragment dialogFragment = new CustomDialog();
        Bundle bundle = new Bundle();
       // I am getting data from intent.
        bundle.putSerializable(key, data);
        dialogFragment.setArguments(bundle);
        dialogFragment.show(transaction, "dialog");
    
;

如果应用程序在前台,我可以看到包含所有数据的对话框。如果应用处于后台或应用未运行,则此代码不起作用。

我试图寻找解决方案,说明获取应用程序的运行状态并相应地设置一些 if 条件。我想知道是否有其他解决方案。 谢谢。

【问题讨论】:

【参考方案1】:

简单地将活动制作为对话框并使用它。

为您的活动设置对话框主题:

<activity name=".MyDialogActivity" android:theme="Theme.AppCompat.Dialog"/>

【讨论】:

我不想将我的活动显示为对话框。请仔细阅读我的问题。谢谢。 我知道你的要求。你不能在没有活动的情况下显示对话框。创建 Activity 而不是 DialogFragment。 我已经有活动了。我的第二段代码在 Activity 中。 我想在当前活动的顶部显示对话框。 我完全了解您的实施。您已经在活动中实现了广播接收器以显示对话框片段。但我要说的是创建一个活动并将其显示为对话框。直接从 Firebase 消息服务启动对话活动

以上是关于单击推送通知后,当应用程序进入前台时如何显示自定义对话框的主要内容,如果未能解决你的问题,请参考以下文章