简体   繁体   English

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

[英]Is there anyway I can prevent a push notification from displaying on the client side?

I'm using UrbanAirship for push notifications. 我正在使用UrbanAirship进行推送通知。 Everything is working fine, but there are certain scenarios where I wouldn't like to display the push notification coming from urban. 一切正常,但是在某些情况下,我不想显示来自市区的推送通知。 Do I have to handle this on the server side or can I handle it on the client side? 我必须在服务器端处理还是在客户端处理?

If possible, how would I handle this on the client side? 如果可能,我将如何在客户端处理此问题? I've tried canceling the notification, and that works, but it still displays the rollover message in the status bar. 我尝试取消该通知,并且该方法可行,但是它仍在状态栏中显示翻转消息。

You need to override Airship's notification factory for this: 为此,您需要覆盖飞艇的通知工厂:

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;
        }
    }

}

and when taking off: 起飞时:

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

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

});

You need to create a BroadcastReceiver in your app to intercept the push. 您需要在应用中创建一个BroadcastReceiver来拦截推送。 You can then choose to display a custom notification or not. 然后,您可以选择显示或不显示自定义通知。

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

If you're using Kotlin: 如果您使用的是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()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM