【问题标题】:Show alertbox on GCM notification recieved在收到 FCM 通知时显示警报框
【发布时间】:2014-05-28 19:58:40
【问题描述】:

当我使用 GCM 接收推送通知时,我需要显示带有是和否按钮的警报框。

会有两个场景:

  1. 如果应用程序在后台,用户会在标题栏中收到通知。点击通知后,用户将被定向到应用程序,此处需要显示警报框,用户将有两个选项,即是或否

  2. 如果应用程序在前台,用户会自动获取警报框,需要显示其中用户将有两个选项,即是或否**

请帮助我实现这一目标。

【问题讨论】:

  • 我已经在当前应用中完成了这项工作。
  • @Simple Plan:你能分享我的代码吗?
  • @ACC 直接询问CODE?你先把你的代码贴出来,你现在尝试了什么?
  • @SimplePlan 我也遇到了类似的问题 我已经在这里问了一个问题 如果你已经解决了请指导我stackoverflow.com/questions/23905566/…

标签: android alert google-cloud-messaging android-alertdialog


【解决方案1】:

使用本地事件总线实现,例如 LocalBroadcastManager、greenrobot 的 EventBus 或 Square 的 Otto。当 GCM 消息到达时引发事件。让 UI 层在该事件处于前台时注册该事件,并在它移动到后台时取消注册。如果您确定 UI 层没有响应该事件,则显示 Notification

Here are three sample apps -- 上述每个事件总线实现都有一个 -- 证明了这一点。我使用 AlarmManager 而不是 GCM 消息,但概念保持不变。

【讨论】:

    【解决方案2】:

    首先您需要检查您的应用程序是否在后台。您可以在应用程序中的每个活动的 onPause() 上调用以下代码:

    /**
    * Checks if the application is being sent in the background (i.e behind
    * another application's Activity).
    * 
    * @param context the context
    * @return <code>true</code> if another application will be above this one.
    */
    public static boolean isApplicationSentToBackground(final Context context) {
     ActivityManager am = (ActivityManager)        context.getSystemService(Context.ACTIVITY_SERVICE);
     List<RunningTaskInfo> tasks = am.getRunningTasks(1);
     if (!tasks.isEmpty()) {
     ComponentName topActivity = tasks.get(0).topActivity;
      if (!topActivity.getPackageName().equals(context.getPackageName())) {
      return true;
     }
    }
    
    return false;
    

    }

    如果是,则向用户显示通知。

    在清单文件中添加这一行:

    <uses-permission android:name="android.permission.GET_TASKS" />
    

    关于生成通知,可以查看我的回答here

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-31
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多