Urban Airship:默认状态栏通知的自定义图标

时间:2012-05-19 14:26:44

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

Urban Airship建议您使用CustomPushNotificationBuilder创建自定义通知,以便对状态栏通知进行任何修改,包括轻松更改图标。

不幸的是,使用RemoteView进行通知会带来许多与自定义制造商和/或特定于平台的外观相关的不良影响,including text colors以及对私有资源的引用(例如@*android:drawable/notify_panel_notification_icon_bg_tile关于Honeycomb / ICS)。

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

3 个答案:

答案 0 :(得分:9)

我发现通过覆盖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);

答案 1 :(得分:1)

我知道这是一个老问题,但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()));

答案 2 :(得分:1)

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

    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