简体   繁体   English

将活动与broadCast Receiver问题同步

[英]Sync Activity with broadCast Receiver issue

I wanna sync my Activity with Google cloud Messaging. 我想将我的活动与Google云消息传递同步。 When GCM message Receives Its own receiver get the message and create notification and then broadcast my custom message to activity receiver. 当GCM消息收到时,它自己的接收者获取消息并创建通知,然后将我的自定义消息广播到活动接收者。

In other hand my Activity has own dynamically registered BroadcastReceiver that Receives my cusom messsages. 另一方面,我的活动具有自己动态注册的BroadcastReceiver,用于接收我的提示消息。

now this is a situation: 现在这是一种情况:

  • when app is open, without clicking on notification, my activity receiver receives the message and shows. 当应用打开时,没有单击通知,我的活动接收者会收到消息并显示。
  • but when activity is closed after clicking on notification noting receives and app just opens. 但是当点击通知后关闭活动时,注意接收并仅打开应用程序。

I tried may ways like: 我尝试了以下方式:

  1. register a class BroadCast receiver On maifest. 注册一个类BroadCast接收器。 but I cannot sync it with my activity. 但我无法将其与我的活动同步。 cause I found that outer Receiver can sysnc with activity just with putextra and then my activity should close and then open again to can get extras! 因为我发现外部Receiver可以仅使用putextra进行sysnc活动,然后我的活动应关闭,然后再次打开才能获得额外收益!
  2. try to broadcast my custom message again on creates notification, but it seems wont work cause I need to broad cast on NotificationClick not onCreate notification. 尝试在创建通知时再次广播我的自定义消息,但似乎无法正常工作,因为我需要对NotificationClick进行广泛的投射,而不是onCreate通知。
  3. finnaly I tried to Register this internal reciver dynamically on another part of but It cannot reachable. finnaly,我尝试在的另一部分上动态注册此内部接收器,但无法访问。

so if you get my problem what is the best way of solving? 因此,如果您遇到我的问题,最好的解决方法是什么?

this is my in activity receiver: 这是我的活动接收者:

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("LOG", "unreciver");
            String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
            // Waking up mobile if it is sleeping
            WakeLocker.acquire(getApplicationContext());

            /**
             * Take appropriate action on this message
             * depending upon your app requirement
             * For now i am just displaying it on the screen
             * */

            //Showing received message
            //lblMessage.append(newMessage + "\n");
            Log.i("LOG", "unreciver messsage:"+newMessage);
            //Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
            loadReciverDialog(newMessage);
            // Releasing wake lock
            WakeLocker.release();
        }
    };

this is the part of service that receive message from GCM and Create notification: 这是从GCM接收消息并创建通知的服务的一部分:

     @Override
        protected void onMessage(Context context, Intent intent) {

            Log.i(TAG, "Received message");
            String message = intent.getExtras().getString("price");

            Log.i("LOG", "GCM service Message "+message);

            displayMessage(context, message);
            // notifies user
            generateNotification(context, message);
        }

 private static void generateNotification(Context context, String message) {

        Log.i("LOG", "genetaret notify");
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context, MainActivity.class);

        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");


        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);      

    }

}

and this part display message: 这部分显示消息:

  static void displayMessage(Context context, String message) {
        Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
        intent.putExtra(EXTRA_MESSAGE, message);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Log.i("LOG", "commonutils msg="+message);
        context.sendBroadcast(intent);

    }

I finally solve this problem in this way: 我终于以这种方式解决了这个问题:

  • I kept the in-Activity Broadcast Receiver and made a function for checking if Extras exist my codes under OnReceive functions works. 我保留了活动中的广播接收器,并创建了一个用于检查是否存在Extras的函数,我在O​​nReceive函数下的代码是否有效。
  • after create notification put message to extra for Main Activity to get them 创建通知后,向主活动发送额外消息以获取它们

so my app get message in two way: 所以我的应用程序通过两种方式获取消息:

  1. when app is opens message will gave with receiver 当应用程序打开时,消息将与接收者一起给出
  2. when app is closed message will gave to app by extra 当应用关闭时,消息会额外发送给应用

but I wanna know if there is a better way to work with one Receiver. 但我想知道是否有更好的方法与一个Receiver一起工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM