【问题标题】:Urban Airship: custom icon for default status bar notificationUrban Airship:默认状态栏通知的自定义图标
【发布时间】:2012-05-26 19:38:40
【问题描述】:

如果您想对状态栏通知进行任何修改,包括简单地更改图标,Urban Airship 建议使用 CustomPushNotificationBuilder 创建自定义通知。

不幸的是,使用RemoteView 通知会带来许多与定制制造商和/或特定平台皮肤、including text colors 和对私有资源(例如 Honeycomb/ICS 上的@*android:drawable/notify_panel_notification_icon_bg_tile)相关的不必要的影响。

必须有一种简单的方法来交换图标而不使用RemoteView。怎么样?

【问题讨论】:

    标签: android urbanairship.com android-notifications android-notification-bar


    【解决方案1】:

    我发现通过覆盖BasicPushNotificationBuilder,我可以非常简单地设置图标:

    BasicPushNotificationBuilder nb = new BasicPushNotificationBuilder() {
        @Override
        public Notification buildNotification(String alert,
                Map<String, String> extras) {
            Notification notification = super.buildNotification(alert,
                    extras);
            // The icon displayed in the status bar
            notification.icon = R.drawable.notification;
            // The icon displayed within the notification content
            notification.contentView.setImageViewResource(
                    android.R.id.icon, R.drawable.notification);
            return notification;
        }
    };
    // Set the custom notification builder
    PushManager.shared().setNotificationBuilder(nb);
    

    【讨论】:

    • 嗨,Paul,我即将处理自定义图标并修改插件,以便在出现多个通知时仅显示单个图标。您的代码示例看起来是一个很好的起点,我想说感谢您回答您自己的问题。我想知道您是否可以阐明在何处覆盖此代码?我对 Android 开发很陌生,我的应用程序正在使用 Phonegap。结果我真的不知道这段代码会去哪里。任何帮助都会很棒。谢谢!
    • @RyanMartin 修改原始库无需动手。您可以通过将上述代码放在应用程序的onCreate() 中来覆盖该功能。您需要定义自己的扩展Application 的类,并使用&lt;application&gt; 中的android:name="..." 属性在清单中声明它。要使所有通知显示在一个图标下,一个好的开始是设置一个固定的通知 ID。看看你能走多远,如果你有任何困难,可以随意放置一个新问题的链接。
    • 节省了我大概一个小时的猜测和检查时间。谢谢!
    • 您在应用程序生命周期的哪个位置调用它?
    【解决方案2】:

    我知道这是一个老问题,但 UrbanAirship 经常更新,所以我决定帮助可能访问此页面的其他人。从 6.0.1 版开始,不再有 BasicNotificationBuilder。为了使用图标和颜色等自定义通知,您需要扩展 NotifcationFactory 类,并覆盖 createNotification 方法。

    如下例所示:

    public class MyNotificationFactory extends NotificationFactory {
    
    public MyNotificationFactory(Context context){
        super(context);
    }
    
    @Override
    public Notification createNotification(PushMessage pushMessage, int i) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext())
                .setContentTitle(getContext().getResources().getString(R.string.app_name))
                .setContentText(pushMessage.getAlert())
                .setSmallIcon(R.drawable.your_icon_here)
                .setColor(getContext().getResources().getColor(R.color.your_color_here))
                .setAutoCancel(true);
    
        return builder.build();
    }
    
    @Override
    public int getNextId(PushMessage pushMessage) {
        return NotificationIDGenerator.nextID();
    }
    

    }

    最后,您必须在您的应用程序类或您初始化 UA 的任何位置将其设置为 UrbanAirship 的新通知工厂:

    UAirship.shared().getPushManager().setNotificationFactory(new MyNotificationFactory(getApplicationContext()));
    

    【讨论】:

    • 如何在城市飞艇通知图标点击显示应用?
    【解决方案3】:

    我们在默认通知工厂中提供了许多功能,例如大样式(收件箱、文本、图像)、棒棒糖功能(隐私、优先级)和交互式通知按钮。如果你只是想设置图标,也许是强调色,我建议如下:

        UAirship.takeOff(this, new UAirship.OnReadyCallback() {
            @Override
            public void onAirshipReady(UAirship airship) {
                // Perform any airship configurations here
    
                // Create a customized default notification factory
                DefaultNotificationFactory defaultNotificationFactory = new DefaultNotificationFactory(getApplicationContext());
                defaultNotificationFactory.setSmallIconId(R.drawable.ic_notification);
                defaultNotificationFactory.setColor(NotificationCompat.COLOR_DEFAULT);
    
                // Set it
                airship.getPushManager().setNotificationFactory(defaultNotificationFactory);
            }
        });
    

    课程的完整文档可以在这里找到 - http://docs.urbanairship.com/reference/libraries/android/latest/reference/com/urbanairship/push/notifications/DefaultNotificationFactory.html

    【讨论】:

    • 完美答案。他们为什么不更新他们的文档。与 Urban Airship 合作太粗糙了。糟糕的文档。
    • @Harpreet 我们会努力更新我们的文档。你能否提供更多关于是什么让我们的文档变得糟糕的细节? docs.urbanairship.com/platform/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多