簡體   English   中英

無論如何,我可以阻止在客戶端顯示推送通知嗎?

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM