城市飞艇:默认状态栏通知自定义图标(Urban Airship: custom icon for d

2019-09-17 09:03发布

城市飞艇建议创建一个自定义通知CustomPushNotificationBuilder如果要进行任何修改状态栏通知,包括平凡的改变图标。

不幸的是,使用RemoteView的通知,附有相关的定制制造商和/或特定平台的外观许多不必要的影响, 包括文本颜色和引用私人资源(例如@*android:drawable/notify_panel_notification_icon_bg_tile在蜂窝/ ICS)。

必须有交换的图标,而无需使用一种简单的方式RemoteView 。 怎么样?

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


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

}

最后,则必须将此作为UrbanAirship的新通知工厂在您的应用程序类或任何你初始化UA:

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


Answer 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: custom icon for default status bar notification