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