-3
我正在开发一个项目,并使用带有wakefulBroadcastReceiver的Intent Service来安排警报并发送通知。目前没有问题。但是当我打开我的应用程序时,它不会再显示通知。请有任何想法解决这个问题?通知Android:当应用程序打开时不显示通知?
我正在开发一个项目,并使用带有wakefulBroadcastReceiver的Intent Service来安排警报并发送通知。目前没有问题。但是当我打开我的应用程序时,它不会再显示通知。请有任何想法解决这个问题?通知Android:当应用程序打开时不显示通知?
在您的通知收件人收到通知时,您可以检查应用程序是否在后台并做出相应显示通知的决定。
这里有一个代码来检查应用程序是否在后台。
public class MyGcmPushReceiver extends GcmListenerService {
/**
* Called when message is received.
* @param from SenderID of the sender.
* @param bundle Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
@Override
public void onMessageReceived(String from, Bundle bundle) {
// Check here whether the app is in background or running.
if(isAppIsInBackground(getApplicationContext())) {
// Show the notification
} else {
// Don't show notification
}
}
/**
* Method checks if the app is in background or not
*/
private boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
}
else
{
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
return isInBackground;
}
}
我打电话给Method里面onCreate活动?? @先生。兔子 –
是来电是来自活动,但发布在应用程序类 – user3040153
@FinnD您需要在您的接收器,而不是在onCreate调用此方法。如果您使用的是GCM接收器,我已经编辑了可以显示GCM接收器示例的答案。 –