Я пытаюсь научиться использовать городской дирижабль для отправки push-уведомлений, и для этого я создал очень простое фиктивное приложение, которое его использует.
Приложение имеет собственный автопилот и собственную фабрику уведомлений для тестирования некоторых функций. Все это отлично работает на андроидах с версиями API> 19.
Однако для устройств с версией 19 (самая низкая, которую мне нужно поддерживать) автопилот никогда не инициализируется, и поэтому всякий раз, когда я пытаюсь получить доступ к UAutopilot.shared()...
, приложение вылетает с ошибкой.
взлет должен быть объявлен до того, как поделится
Даже вызов Autopilot.autoTakeoff(application)
не решает эту проблему.
Основная деятельность:
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());
}
});
}
}
Пользовательский автопилот:
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();
}
}