Notification Android : 应用打开时不显示通知?
Notification Android : Don't show notification when app is open?
我正在做一个项目,我使用 Intent Service 和 wakefulBroadcastReceiver 来安排警报和发送通知。到目前为止没有问题。但我希望当我打开我的应用程序时,它不再显示通知。请问这个问题有什么想法吗?
当您收到通知时,在您的通知接收器中,您可以检查该应用程序是否在后台,并相应地决定是否显示通知。
这是检查应用程序是否处于后台的代码。
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;
}
}
我正在做一个项目,我使用 Intent Service 和 wakefulBroadcastReceiver 来安排警报和发送通知。到目前为止没有问题。但我希望当我打开我的应用程序时,它不再显示通知。请问这个问题有什么想法吗?
当您收到通知时,在您的通知接收器中,您可以检查该应用程序是否在后台,并相应地决定是否显示通知。
这是检查应用程序是否处于后台的代码。
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;
}
}