Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我是否可以阻止推送通知显示在客户端?_Java_Android_Urbanairship.com - Fatal编程技术网

Java 我是否可以阻止推送通知显示在客户端?

Java 我是否可以阻止推送通知显示在客户端?,java,android,urbanairship.com,Java,Android,Urbanairship.com,我正在使用UrbanAirship发送推送通知。一切正常,但在某些情况下,我不想显示来自市区的推送通知。我必须在服务器端处理还是可以在客户端处理 如果可能,我将如何在客户端处理此问题?我已尝试取消通知,但它仍然会在状态栏中显示滚动消息。您需要在应用程序中创建一个广播接收器来拦截推送。然后,您可以选择是否显示自定义通知 查看安装文档您需要覆盖飞艇的通知工厂: package your.package; import android.app.Notification; import android

我正在使用UrbanAirship发送推送通知。一切正常,但在某些情况下,我不想显示来自市区的推送通知。我必须在服务器端处理还是可以在客户端处理


如果可能,我将如何在客户端处理此问题?我已尝试取消通知,但它仍然会在状态栏中显示滚动消息。

您需要在应用程序中创建一个
广播接收器来拦截推送。然后,您可以选择是否显示自定义通知


查看安装文档

您需要覆盖飞艇的通知工厂:

package your.package;

import android.app.Notification;
import android.content.Context;
import android.support.annotation.NonNull;

import com.urbanairship.push.PushMessage;
import com.urbanairship.push.notifications.DefaultNotificationFactory;

public class PushNotificationFactory extends DefaultNotificationFactory {

    public PushNotificationFactory(Context context) {
        super(context);
    }

    public Notification createNotification(@NonNull PushMessage message, int notificationId) {
        boolean shouldWeShowNotification = false; // your condition goes here
        if (shouldWeShowNotification) {
            return super.createNotification(message, notificationId);
        } else {
            return null;
        }
    }

}
起飞时:

UAirship.takeOff(this, new UAirship.OnReadyCallback() {

    @Override
    public void onAirshipReady(UAirship airship) {
        NotificationFactory notificationFactory = new PushNotificationFactory(getApplicationContext());
        airship.getPushManager().setNotificationFactory(notificationFactory);
    }

});

如果您正在使用Kotlin:

class PushNotificationFactory(context: Context) : NotificationFactory(context) {

    override fun createNotification(message: PushMessage, notificationId: Int): Notification? {
        val notificationWillBeShowed = false // your condition goes here
        return if (notificationWillBeShowed) {
            // Show notification
            super.createNotification(message, notificationId)
        } else {
            // Prevent notification from showing
            null
        }
    }
}


class UrbanAirshipAutopilot : Autopilot()  {

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onAirshipReady(airship: UAirship) {
        airship.pushManager.userNotificationsEnabled = true
        val context = UAirship.getApplicationContext()
        val notificationFactory = PushNotificationFactory(context)
        airship.pushManager.notificationFactory = notificationFactory
        if (Build.VERSION.SDK_INT >= 26) {
            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            val defaultChannelId = context.getString(R.string.notification_channel_id_default)
            val defaultChannelName = context.getString(R.string.notification_channel_name_default)
            val channel = NotificationChannel(defaultChannelId, defaultChannelName, NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager.createNotificationChannel(channel)
        }
    }

    override fun createAirshipConfigOptions(context: Context): AirshipConfigOptions? {
        val defaultChannelId = context.getString(R.string.notification_channel_id_default)
        return AirshipConfigOptions.Builder()
                .setDevelopmentAppKey(BuildConfig.URBAN_AIRSHIP_APP_KEY_DEVELOPMENT)
                .setDevelopmentAppSecret(BuildConfig.URBAN_AIRSHIP_APP_SECRET_DEVELOPMENT)
                .setProductionAppKey(BuildConfig.URBAN_AIRSHIP_APP_KEY_PRODUCTION)
                .setProductionAppSecret(BuildConfig.URBAN_AIRSHIP_APP_SECRET_PRODUCTION)
                .setInProduction(!BuildConfig.DEBUG)
                .setGcmSender(BuildConfig.CLOUD_MESSAGING_SENDER_ID)     // FCM/GCM sender ID
//                .setNotificationIcon(R.drawable.ic_notification)
//                .setNotificationAccentColor(ContextCompat(getContext(), R.color.accent))
                .setNotificationChannel(defaultChannelId)
                .build()
    }
}

PushNotificationBuilder似乎是一个您可能需要查看的界面。如果看到推送消息,则不希望显示的通知,在其buildNotification方法中可以返回null;--我对UrbanAirship完全没有经验,不过我只是看了一下他们的API文档。你是在编写本地Android代码,还是在使用phonegap之类的软件?@harism,谢谢,看起来这会有所帮助。我现在正在看它。@CarlAnderson,yessir,我正在写原生android代码。我已经分析了标签。UrbanAirship是客户端还是服务器端?我的应用程序使用BroadcastReceiver子类实际创建发送到托盘上的通知,但不清楚您是否有类似的设置。这是我最初的想法,但我发现这只允许您对接收、注册或打开的操作执行一些代码块。不能阻止它打开。除非我遗漏了什么?我已经在一个工作项目中解决了这个问题,但要到明天才能检查代码。一旦我的
BroadcastReceiver
PushManager.ACTION\u PUSH\u RECEIVED
操作匹配,我就会创建一个自定义通知。如果我没有从这里运行自定义通知代码,那么我就看不到通知。如果仍未解决,将在明天更新。