GCM IntentService how to display a pop up on notification receive

前端 未结 2 946
旧巷少年郎
旧巷少年郎 2021-01-15 08:09

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

I wanted to access my current activity in GcmIntentService but I do

2条回答
  •  星月不相逢
    2021-01-15 08:19

    I have something like this:

    In your GCM broadcast receiver

    Intent intent = new Intent(NOTIFICATION_ACTION);
    intent.putExtra(EXTRA_PARAMETER, "something you want to pass as an extra");
    context.sendBroadcast(intent);
    

    In your BaseActivity (an Activity that is extended by all the activities that you want to show the pop up)

    private BroadcastReceiver notificationBroadcastReceiver = new NotificationBroadcastReceiver();
    
    @Override
    protected void onStart() {
        super.onStart();
        IntentFilter intentFilter = new IntentFilter(NOTIFICATION_ACTION);
        registerReceiver(notificationBroadcastReceiver, intentFilter);
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(notificationBroadcastReceiver);
    }
    
    private class NotificationBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Show popup
        }
    }
    

    NOTIFICATION_ACTION is a constant you should define somewhere

提交回复
热议问题