问题
I use NotificationManager, NotificationChannel and NotificationCompat.Builder to display a push notification when an event is triggered, no online Firebase.
The push notification appears when my app is in foreground and background, but it only should appear when my app in the background.
I have 2 Activities where just only 1 knows the class, where the push notification is created. I could just set a variable that shows if my Activity is in foreground or not (with onResume and onPause), but if my other Activity starts the Activity with the notification trigger class is set in background and therefore I can't use this method. I would need to know if the whole app is in foreground or not, but I didn't find a good solution for that problem.
So is there an other way to display push notifications just when my whole app is in background?
回答1:
I think you have FCMListenerService
to receive your push notification as a background service. The Service
should have the following declaration in the manifest file.
<service android:name=".Service.FCM.FCMListenerService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Now you can pass the context of this service alone to check if the application is in foreground or in background. The function for checking if the application is foreground is following.
private static boolean isForeground(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
return componentInfo.getPackageName().equals(Constants.ApplicationPackage);
}
Hence, in your onMessageReceived
function you need to check if the application is in foreground and do not create the notification in the status bar.
public class FCMListenerService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message) {
if(!isForeground(this)) createNotification();
}
}
Hope that helps!
回答2:
So I found a proper and very good solution for this problem. According to this answer: https://stackoverflow.com/a/42679191/6938043
Application.ActivityLifecycleCallbacks
public class MainActivityChats extends AppCompatActivity implements Application.ActivityLifecycleCallbacks
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getApplication().registerActivityLifecycleCallbacks(this);
}
public void onActivityResumed(Activity activity)
{
if(msg_updater != null)
{
msg_updater.set_app_is_active(true);
}
}
public void onActivityPaused(Activity activity)
{
if(msg_updater != null)
{
msg_updater.set_app_is_active(false);
}
}
public void onActivityStopped(Activity activity) {}
public void onActivityCreated(Activity activity, Bundle bundle) {}
public void onActivityStarted(Activity activity) {}
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}
public void onActivityDestroyed(Activity activity) {}
}
来源:https://stackoverflow.com/questions/48965528/sending-a-push-notification-only-when-app-is-in-background