[英]GCM Android Notification When App Is Open
我已经正确设置了适用于Android的Google Cloud Messaging,并从服务器接收通知。 但是,我想避免在打开应用程序时发出通知,但会获取数据并在应用程序内通知用户。 请查看下面的我的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);
}
我不能从MainActivity引用函数,因为它们是非静态的。 常用的方法是什么? 我很难找到与此情况有关的任何问题。
我建议您看一下: 检查Android应用程序是否在后台运行有多个“ hacks”可以知道该应用程序是否在后台运行,其中之一是考虑到正在运行的进程。活动经理。 另一个是跟踪活动生命周期。 在我提供的链接中,您将可以获得更多详细信息。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.