问题
Urban Airship recommends creating a custom notification with CustomPushNotificationBuilder
if you want to make any modifications to the status bar notification, including trivially changing the icon.
Unfortunately, using a RemoteView
for notifications carries many unwanted implications with it related to custom manufacturer and/or platform-specific skins, including text colors and references to private resources (for instance @*android:drawable/notify_panel_notification_icon_bg_tile
on Honeycomb/ICS).
There must be a simple way to swap the icon without using RemoteView
. How?
回答1:
I found that by overriding BasicPushNotificationBuilder
, I can set the icon quite trivially:
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);
回答2:
I know this is an old question, but UrbanAirship get's updated quite often, so I decided to help others who might reach this page. As of version 6.0.1 there's no BasicNotificationBuilder
no more. In order to customize your notification with icon and color and whatnot, you need to extend the NotifcationFactory
class, and override the createNotification
method.
Like shown in the example below:
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();
}
}
At last you must set this as UrbanAirship's new notification factory in your application class or wherever you initialized UA:
UAirship.shared().getPushManager().setNotificationFactory(new MyNotificationFactory(getApplicationContext()));
回答3:
We provide a lot of functionality in our default notification factory such as big styles (inbox, text, image), lollipop features (privacy, priority), and interactive notification buttons. If you are only trying to set the icon and maybe the accent color, I recommend the following:
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);
}
});
Full docs on the class can be found here - http://docs.urbanairship.com/reference/libraries/android/latest/reference/com/urbanairship/push/notifications/DefaultNotificationFactory.html
来源:https://stackoverflow.com/questions/10666022/urban-airship-custom-icon-for-default-status-bar-notification