Refreshing activity on receiving gcm push notification

后端 未结 7 1167
自闭症患者
自闭症患者 2020-11-29 16:45

Update: GCM is deprecated, use FCM

How to refresh activity on receiving gcm push notification<

相关标签:
7条回答
  • 2020-11-29 17:11

    To sum it up in single sentence: If you want to refresh activity, broadcast your custom event when notification arrives and register your activity as broadcast receiver of that event

    0 讨论(0)
  • 2020-11-29 17:16
    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
    PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    0 讨论(0)
  • 2020-11-29 17:25

    The accept answer is indeed correct for the "Refreshing activity on receiving gcm push notification" (I've upvoted it too). But if you only want to update a ListView that's being displayed you don't need a broadcast receiver.

    Your GCM listener service can update the database using a ContentProvider rather than inserting a direct sql query.

    Then you can rely on the notifyChange method on the ContentResolver to do the trick.

    Notify registered observers that a row was updated. To register, call registerContentObserver(). By default, CursorAdapter objects will get this notification. If syncToNetwork is true, this will attempt to schedule a local sync using the sync adapter that's registered for the authority of the provided uri. No account will be passed to the sync adapter, so all matching accounts will be synchronized.

    0 讨论(0)
  • 2020-11-29 17:32

    If your app is already running then try to override the onNewIntent method

    0 讨论(0)
  • 2020-11-29 17:33

    Took me a few hours to figure it out. Posting here in case anyone anyone else has the same problem.

    The idea is that you have to register your activity as a broadcast receiver. The easiest way to do this is like so:

    //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
        }
    };
    

    The above code goes in the activity that you want to 'listen' for events.

    Now, how do we send data to this 'listener'? Go to your push notification handler(or from where you want to update your activity) and when you receive a notification call this function:

    // 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);
    }
    

    When you call the above function, your activity should receive it.

    Note: Your activity must be running/open to receive the broadcast intent

    Note2: I switched to a library called 'otto'. It does actually the same thing but easier, 'broadcasts events' thoughout the app. Here's a link http://square.github.io/otto/

    0 讨论(0)
  • 2020-11-29 17:34

    I'm assuming your GCMBroadcastReceiver is in it's own .java file?

    As far as refreshing an activity, I would also like to know the answer to that question.

    But for knowing if a particular activity is active or not, meaning on screen just add a boolean (call it something like "active") and set it to true in your activity's onResume() event, and to false in the onPause() event:

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

    Your active variable would be a boolean which is global or static. This way you know if a particular activity is in "front".

    Hope that helps a bit.

    0 讨论(0)
提交回复
热议问题