使用应用程序时(在前台运行)请勿调用推送通知 [英] Don't invoke push notification when app is being used (running in foreground)

查看:97
本文介绍了使用应用程序时(在前台运行)请勿调用推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用中,我通过Firebase Cloud Messaging接收了推送通知.通知工作完全正常,唯一的问题是我不希望在应用程序运行时接收通知.当用户使用ap时,我已经在内部显示了通知,因此推送通知既多余又烦人.以下是有关我如何调用推送通知的代码:

In my android app, I receive push notifications via Firebase Cloud Messaging. The notifications work completely fine, the only problem is that I don't want to receive notifications when the app is running. When the user is using the ap, I am already displaying the notification internally so the push notification is redundant and annoying. Below is the code on how I invoke push notification:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setVibrate(pattern)
            .setLights(Color.BLUE,1,1)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notificationId, notificationBuilder.build());
}

我在此链接上尝试了该解决方案,但由于我确实希望收到通知,因此在我的情况下它不起作用当应用程序在后台或被杀死时.另外,在应用程序启动时将布尔值设置为true,在应用程序关闭时将布尔值设置为false似乎不是理想的解决方案.必须有更好的方法,但是我找不到.任何帮助将不胜感激.

I tried the solution on this link, and it won't work in my case because I do want the notification when the app is in the background or killed. Also setting a boolean to true when the app starts and false when the app is closed does not seem like an ideal solution. There has to be a better way but I can't find it. Any help would be appreciated.

推荐答案

我要做的是在Application类中使用ActivityLifeCycleCallbacks

What I do is use ActivityLifeCycleCallbacks in your Application class

class MyApplication:Application(){
companion object {
        private var activitiesOpen = 0

        var displayActivity:String?= null
        private set

        fun isAppOpen() = activitiesOpen > 0
    }
    private val activitiesListener = object : ActivityLifecycleCallbacks{
        override fun onActivityPaused(activity: Activity?) {
            --activitiesOpen
            displayActivity = null
        }

        override fun onActivityResumed(activity: Activity?) {
            activitiesOpen++
            displayActivity = if(activity != null)
                activity::class.simpleName else null
        }

        override fun onActivityStarted(activity: Activity?) {
        }

        override fun onActivityDestroyed(activity: Activity?) {
        }

        override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
        }

        override fun onActivityStopped(activity: Activity?) {
        }

        override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
        }
    }

}

现在,您可以在 onMessageReceived 中查看它了.

Now in your onMessageReceived you can check it out like this.

    if (MyApplication.isAppOpen() &&
                DashboardActivity::class.simpleName?
.equals(MyApplication.displayActivity) ?: false) {
// you can eliminate the last condition if you wish

            playAlertTone(context) // or whatever
        } else {
           // your notification building code
        }

代码在kotlin中,希望您不会有问题.

The code is in kotlin, I hope you'll have no problem with it.

这篇关于使用应用程序时(在前台运行)请勿调用推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆