3

I'm trying to learn how to use urban airship to send push notifications, and for that, I created a very simple dummy app that uses it.

The app has a custom autopilot and a custom notification factory in order to test some features. It all works fine on androids with api versions >19.

However, for devices with version 19 (the lowest I need to support) the autopilot is never initialized and so, whenever I try to access UAutopilot.shared()... the app crashes with error

takeoff must be called before shared

Even calling Autopilot.autoTakeoff(application) doesn't solve it.

MainActivity:

public class MainActivity extends AppCompatActivity{

    TextView mTextView;
    Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.text);
        mButton = (Button)findViewById(R.id.button2);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextView.setText(UAirship.shared().getPushManager().getChannelId());
            }
        });
    }
}

CustomAutopilot:

class CustomAutopilot extends Autopilot {

@Override
public void onAirshipReady(UAirship airship) {
    Logger.info("onAirshipReady");
    airship.getPushManager().setUserNotificationsEnabled(true);
    airship.getPushManager().setNotificationFactory(new CustomNotificationFactory(UAirship.getApplicationContext()));
}

@Nullable
@Override
public AirshipConfigOptions createAirshipConfigOptions(@NonNull Context context) {
    Logger.info("setting airship config options");
    AirshipConfigOptions options = new AirshipConfigOptions.Builder()
            .setDevelopmentAppKey("xxxxxxxxxxx")
            .setDevelopmentAppSecret("xxxxxxxxxxx")
            .setDevelopmentLogLevel(Log.DEBUG)

            .setInProduction(false)

            .setGcmSender("232973289571")

            .setNotificationIcon(R.drawable.icon)
            .setNotificationAccentColor(Color.rgb(0, 72, 51))
            .build();
        return options;
    }
}

customNotificationFactory:

public class CustomNotificationFactory extends NotificationFactory {
    public CustomNotificationFactory(@NonNull Context context) {
        super(context);
    }

    @Nullable
    @Override
    public Notification createNotification(@NonNull PushMessage message, int notificationId) {
        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(getContext())
                .setContentTitle("altered " + message.getTitle())
                .setContentText(message.getAlert())
                .setSmallIcon(message.getIcon(getContext(), R.drawable.icon))
                .setColor(Color.rgb(212, 45, 198))
                .setVibrate(new long[]{100, 50, 100, 200, 100, 50, 100})
                .setPriority(Notification.PRIORITY_MAX);

        return builder.build();

    }
}
ricardo silva
  • 331
  • 1
  • 18

1 Answers1

5

Are you registering the autopilot in your manifest?

In the application entry:

    <!-- Autopilot calls takeOff without the need to override the Application -->
    <meta-data
        android:name="com.urbanairship.autopilot"
        android:value="your.package.here.CustomAutopilot"/>
ralepinski
  • 1,756
  • 8
  • 15
  • I'm doing that, yes. I found out that for some reason, if I don't have an `airshipconfig.properties` file, it messes up kit kat and auto pilot doesn't work at all. So what I did was I created that file and then used my custom autopilot to override what I needed – ricardo silva Jul 14 '17 at 10:13
  • 1
    Sounds like you might have the meta-data entry wrong. Make sure its inside the application section of the manifest and the value is your package name + class name. The classes package should be at the top of the file. – ralepinski Jul 18 '17 at 00:18
  • That can't be it, for two reasons: it works fine in other api versions other than 19; if i create an `airshipconfig.properties` file, even if i overwrite everything in my custom autopilot, it works on every version, including api 19 – ricardo silva Jul 18 '17 at 09:33
  • 1
    I've got this error after changing my app package name. forgot to update package in – Abdul Basit Rishi Jan 21 '20 at 06:49