【问题标题】:Is there anyway I can prevent a push notification from displaying on the client side?无论如何我可以阻止推送通知在客户端显示?
【发布时间】:2014-06-10 18:29:08
【问题描述】:

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

如果可能,我将如何在客户端处理这个问题?我试过取消通知,效果很好,但它仍然在状态栏中显示翻转消息。

【问题讨论】:

  • PushNotificationBuilder 似乎是您可能想看看的界面。如果你看到一条推送消息,你不想显示通知,在它的 buildNotification 方法中你可以返回 null ; docs.urbanairship.com/reference/libraries/android/latest/… -- 我完全没有使用 UrbanAirship 的经验,虽然只是查看了他们的 API 文档
  • 您是在编写原生 Android 代码,还是使用 phonegap 之类的东西?
  • @harism,谢谢,看来这会有所帮助。我现在正在看。
  • @CarlAnderson,是的,我正在编写原生 android 代码。我已经更新了标签。
  • UrbanAirship 是在客户端还是服务器端?我的应用程序使用 BroadcastReceiver 子类来实际创建进入托盘的通知,但不清楚您是否有类似的设置。

标签: java android urbanairship.com


【解决方案1】:

您需要为此覆盖 Airship 的通知工厂:

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

});

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这是我最初的想法,但我发现这只允许您在 RECEIVED、REGISTERED 或 OPENED 操作上执行某些代码块。不阻止它打开。除非我错过了什么?
    • 我已经在一个工作项目中解决了这个问题,但直到明天才能检查代码。一旦我的BroadcastReceiver 匹配PushManager.ACTION_PUSH_RECEIVED 操作,我就会创建一个自定义通知。如果我不从这里运行自定义通知代码,那么我看不到通知。如果还没有解决,明天再更新。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2011-07-30
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多