我正在尝试学习如何使用城市飞艇发送推送通知,为此,我创建了一个非常简单的虚拟应用程序,使用它。
该应用有一个自定义自动驾驶仪和一个自定义通知工厂,以测试一些功能。这一切都适用于api版本> 19的机器人。
但是,对于版本为19(我需要支持的最低版本)的设备,自动驾驶永远不会被初始化,因此,每当我尝试访问UAutopilot.shared()...
时,应用程序都会因错误而崩溃
必须在共享之前调用起飞
即使调用Autopilot.autoTakeoff(application)
也无法解决问题。
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();
}
}
答案 0 :(得分:1)
您是否在清单中注册了自动驾驶仪?
在申请表中:
<!-- Autopilot calls takeOff without the need to override the Application -->
<meta-data
android:name="com.urbanairship.autopilot"
android:value="your.package.here.CustomAutopilot"/>