首页 文章

接收gcm推送通知时刷新活动

提问于
浏览
50

更新:不推荐使用GCM,请使用FCM

如果我的应用是 open ,如何在 receiving gcm push notificationrefresh 活动 . 我有一个活动,其中包含从服务器填充数据的列表视图 . 如果我收到gcm推送通知(还包含一些数据),我想刷新我的活动( here adding one more item to listview ) .

  • 一种替代方法是添加 timer ,定期执行服务器请求并更新列表适配器数据,但我不想要这些因为它需要很多资源 .

  • 我是否需要添加 broadcast receiver ,这会在接收gcm push时触发,这会进一步请求更新的服务器数据并更新我的活动UI?

亲爱的评论员,请仔细阅读问题,我只需要 refresh the list (如果应用是开放的,特定活动是开放的) else no need for same .

7 回答

  • 3

    花了我几个小时来搞清楚 . 发布在这里以防任何人有同样的问题 .

    这个想法是你必须将你的活动注册为广播接收者 . 最简单的方法是:

    //register your activity onResume()
    @Override
    public void onResume() {
        super.onResume();
        context.registerReceiver(mMessageReceiver, new IntentFilter("unique_name"));
    }
    
    //Must unregister onPause()
    @Override
    protected void onPause() {
        super.onPause();
        context.unregisterReceiver(mMessageReceiver);
    }
    
    
    //This is the handler that will manager to process the broadcast intent
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
    
            // Extract data included in the Intent
            String message = intent.getStringExtra("message");
    
            //do other stuff here
        }
    };
    

    以上代码包含您要“监听”事件的活动 .

    现在,我们如何向这个“倾听者”发送数据?转到您的推送通知处理程序(或您想要更新活动的位置),当您收到通知时调用此函数:

    // This function will create an intent. This intent must take as parameter the "unique_name" that you registered your activity with
    static void updateMyActivity(Context context, String message) {
    
        Intent intent = new Intent("unique_name");
    
        //put whatever data you want to send, if any
        intent.putExtra("message", message);
    
        //send broadcast
        context.sendBroadcast(intent);
    }
    

    当您调用上述功能时,您的活动应该接收它 .

    Note :您的活动必须正在运行/打开才能接收广播意图

    Note2 :我切换到名为'otto'的库 . 它实际上是相同的事情,但更容易,'broadcasts events'尽管应用程序 . 这是一个链接http://square.github.io/otto/

  • 0

    我假设你的 GCMBroadcastReceiver 在它自己的.java文件中?

    至于刷新活动,我也想知道这个问题的答案 .

    但是要知道某个特定活动是否有效,请在屏幕上添加 boolean (称之为"active")并在活动的 onResume() 事件中将其设置为 true ,并在 onPause() 事件中设置为 false

    protected void onResume()
    {
        super.onResume();
    
        active = true;;
    }
    
    protected void onPause()
    {
        super.onPause();
    
        active = false;
    }
    

    你的 active 变量是一个布尔值,它是 globalstatic . 通过这种方式,您可以了解特定活动是否在"front"中 .

    希望有点帮助 .

  • 138

    接受答案对于“接收gcm推送通知时的刷新活动”确实是正确的(我也赞成了它) . 但是,如果您只想更新正在显示的ListView,则不需要广播接收器 .

    您的GCM侦听器服务可以使用ContentProvider更新数据库,而不是插入直接的SQL查询 .

    然后你可以依靠ContentResolver上的notifyChange方法来完成这个伎俩 .

    通知已注册观察员更新了一行 . 要注册,请调用registerContentObserver() . 默认情况下,CursorAdapter对象将获得此通知 . 如果syncToNetwork为true,则会尝试使用为所提供的uri的权限注册的同步适配器来安排本地同步 . 不会将任何帐户传递给同步适配器,因此将同步所有匹配的帐户 .

  • 2

    如果您的应用已在运行,请尝试覆盖 onNewIntent 方法

  • -4

    似乎有一种更简单的方法 . 在GCM侦听器的OnMessageReceived方法中,您可以从那里进行更新,而不是发送通知 . 您可以使用在处理通知时使用的相同代码 . 如果您正在从侦听器执行StartActivity,则必须使用ActivityFlags.NewTask标志 .

  • 1

    用一句话来概括:如果你想刷新活动, broadcast 你的 custom event when notification arrivesregister 你的 activity 作为那个事件的 broadcast receiver

  • 0
    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
    PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

相关问题