4

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

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

必须有一种简单的方法来交换图标而不使用RemoteView. 如何?

4

3 回答 3

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);
于 2012-05-19T14:29:08.660 回答
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

于 2015-04-17T16:26:57.497 回答
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()));
于 2015-04-17T12:13:13.600 回答