GCM IntentService 如何在收到通知时显示弹出窗口 [英] GCM IntentService how to display a pop up on notification receive

查看:20
本文介绍了GCM IntentService 如何在收到通知时显示弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的应用处于活动状态,我想在收到 GCM 时在我当前的 Activity 上显示一个弹出窗口.

I would like to, on receive of a GCM, display a Popup on my current Activity if my app is active.

我想在 GcmIntentService 中访问我当前的活动,但我认为这不是可能的,也不是一个好方法...

I wanted to access my current activity in GcmIntentService but I dont think it is possible or a good way to proceed...

谁能帮帮我?

解决方案

在我的 GcmIntentService.java 中:

In my GcmIntentService.java :

@Override
protected void onHandleIntent(Intent intent) {
  ...
  Intent broadCastIntent = new Intent("client_notifications_broadcast");
  broadCastIntent.putExtra("data", extras.getString("other"));
  LocalBroadcastManager.getInstance(this).sendBroadcast(broadCastIntent);
  ...
}

在我想要弹出窗口的所有活动扩展的 MainActivity 中,我添加了一个具有自定义布局的对话框:

In my MainActivity extended by all activities where I want the popup I add a Dialog with a custom layout :

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String dataString = intent.getStringExtra("data");

            final Dialog dialog = new Dialog(ClientMainActivity.this);
            dialog.setContentView(R.layout.custom_dialog_popup);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            TextView dialogButton = (TextView) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            dialog.dismiss();
            }
            });
            dialog.show();

            Log.d("receiver", "Got message: " + dataString);
        }
    };

        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("client_notifications_broadcast"));
}

推荐答案

嗯,你可以使用LocalBroadcast.请参阅 LocalBroadcast 管理器.关于如何实现,如何使用LocalBroadcastManager?上有一个很好的例子.

Well, you can use LocalBroadcast. See LocalBroadcast Manager. For how to implement is, there is a great example on how to use LocalBroadcastManager?.

LocalBroadcast Manager 是一个帮助程序,用于注册 Intent 广播并将其发送到进程中的本地对象.您正在广播的数据不会离开您的应用程序,因此无需担心泄露私人数据.`

LocalBroadcast Manager is a helper to register for and send broadcasts of Intents to local objects within your process. The data you are broadcasting won't leave your app, so don't need to worry about leaking private data.`

您的活动注册了此本地广播.从服务中,您从 onMessage 中发送 LocalBroadcast(说嘿,我收到了一条消息.显示它活动).然后在您的 Activity 中,您可以收听广播.这样,如果活动处于最前沿/处于活动状态,它将接收广播,否则不会.因此,每当您收到该本地广播时,如果活动已打开,您就可以执行所需的操作.

Your activity registers for this local broadcast. From the service you send a LocalBroadcast from within the onMessage (saying that hey, I received a message. Show it activity). Then inside your Activity you can listen to the broadcast. This way if the activity is in the forefront/is active, it will receive the broadcast otherwise it won't. So, whenever you receive that local broadcast, you may do the desired action if activity is open.

如果你想为整个应用做,那么你可以让你的所有活动扩展一个抽象活动.在这个抽象活动类中,您可以为这个LocalBroadcast"注册它.另一种方法是在所有活动中注册 LocalBroadcast(但您必须管理仅显示一次消息的方式).

If you want to do for the whole app, then you can make all your activities extend an abstract activity. And inside this abstract activity class you can register it for this 'LocalBroadcast'. Other way is to register for LocalBroadcast inside all your activities (but then you'll have to manage how you'll show the message only once).

希望对您有所帮助.

这篇关于GCM IntentService 如何在收到通知时显示弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆