无论如何,我可以阻止推送通知显示在客户端?

我正在使用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); } }); 

您需要在您的应用程序中创建一个BroadcastReceiver来拦截推送。 您可以选择显示自定义通知。

查看设置文档http://docs.urbanairship.com/build/android_features.html#set-up

如果您使用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() } }