4

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?

Community
  • 1
  • 1
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187

3 Answers3

9

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);
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • Hey Paul, I'm about to tackle custom icons and modify the plugin to only display a single icon should multiple notifications occur. Your code example looks like a great place to start and I wanted to say thanks for answering your own question. I was wondering if you might shed some light on where you're overriding this code? I'm pretty new to Android dev and my application is using Phonegap. As a result I literally don't even know where this code would go. Any help would be great. Thanks! – Ryan Martin Jul 13 '12 at 22:10
  • @RyanMartin No need to get your hands dirty with modifying the original library. You can override the functionality by placing the above code in your application's `onCreate()`. You will need to define your own class that extends `Application`, and declare it in the manifest using the `android:name="..."` attribute in ``. To make all notifications display under a single icon, a good start would be to set a fixed notification ID. See how far you get and feel free to place a link to a new question if you have any difficulties. – Paul Lammertsma Jul 14 '12 at 13:22
  • Saved me probably an hour's worth of guess and check. Thanks! – loeschg Feb 05 '13 at 17:00
  • Where in the application lifecycle do you call this? – Andrew Wyld Feb 21 '13 at 09:56
1

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()));
Zsolt Boldizsar
  • 2,447
  • 2
  • 27
  • 37
1

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

ralepinski
  • 1,756
  • 8
  • 15
  • 1
    Perfect answer. Why didn't they update their docs. It's too rough to work with Urban Airship. Poor docs. – Harpreet Sep 19 '16 at 09:14
  • @Harpreet we try to keep our docs updated. Could you provide more details on what makes our docs poor? http://docs.urbanairship.com/platform/android.html#customizing-push-notifications – ralepinski Sep 19 '16 at 16:34