简体   繁体   English

App开启时的GCM Android通知

[英]GCM Android Notification When App Is Open

I've properly set up Google Cloud Messaging for Android and receive notifications from my server. 我已经正确设置了适用于Android的Google Cloud Messaging,并从服务器接收通知。 However, I'd like to avoid a notification if the app is open but fetch the data and notify the user within the app. 但是,我想避免在打开应用程序时发出通知,但会获取数据并在应用程序内通知用户。 Please look over my GcmIntentService below, where the notification is processed: 请查看下面的我的GcmIntentService,在该处处理通知:

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {  
        if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            if(MainActivity.app_is_open){
                // I would like to call a function from MainActivity to fetch data here 
            } else 
                // Sends notification here
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

I can't reference functions from MainActivity because they are non-static. 我不能从MainActivity引用函数,因为它们是非静态的。 What is the common approach? 常用的方法是什么? I've had trouble finding any questions related to this situation. 我很难找到与此情况有关的任何问题。

I suggest you to have a look at this: Checking if an Android application is running in the background There are several "hacks" to know if the app is running in background, one of them is by taking into account the processes that are running Using the Activity Manager. 我建议您看一下: 检查Android应用程序是否在后台运行有多个“ hacks”可以知道该应用程序是否在后台运行,其中之一是考虑到正在运行的进程。活动经理。 Another one is to keep tracking of the activities lifecycle. 另一个是跟踪活动生命周期。 In the link I provided you'll be able to have more detailed information. 在我提供的链接中,您将可以获得更多详细信息。

Hope this helps. 希望这可以帮助。

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

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