我收到了可怕的“ com.mycompany.applicationame软件包的开发人员警告”。当我向设备发送推送通知时,无法在通道“ null”上发布通知。我正在运行Android API27。这是我的代码:
public class UAAutoPilot extends Autopilot {
@Override
public void onAirshipReady(@NonNull UAirship airship) {
airship.getPushManager().setUserNotificationsEnabled(true);
// Android O
if (Build.VERSION.SDK_INT >= 26) {
Context context = UAirship.getApplicationContext();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("customChannel",
context.getString(R.string.custom_channel),
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
// Create a customized default notification factory
CustomNotificationFactory notificationFactory;
notificationFactory = new CustomNotificationFactory(UAirship.getApplicationContext());
// Set the factory on the PushManager
airship.getPushManager()
.setNotificationFactory(notificationFactory);
airship.getPushManager()
.getNotificationFactory()
.setNotificationChannel("customChannel");
}
}
Logcat消息:
2018-11-14 14:00:52.821 1683-13152 / system_process E / NotificationService:找不到频道 pkg = com.mycompany.applicationame,channelId = null,id = 1007,tag = null, opPkg = com.mycompany.applicationame,调用Uid = 10081,userId = 0, entryUserId = 0,noti
显示通知,但我收到此错误消息。 UrbanAirship的新手。我看不到我在做什么错。
答案 0 :(得分:0)
问题是我没有正确设置Notification Builder。
public class CustomNotificationFactory extends NotificationFactory {
Context context;
String channelID = "";
public CustomNotificationFactory(Context context, String channelID) {
super(context);
this.context = context;
this.channelID = channelID;
}
@Override
public Notification createNotification(PushMessage message, int notificationId) {
// do not display a notification if there is not an alert
if (UAStringUtil.isEmpty(message.getAlert())) {
return null;
}
// Build the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(),channelID) //wasn't sending in channelID here.
.setContentTitle(context.getResources().getString(R.string.notification_title))
.setContentText(message.getAlert())
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_notification);
// Notification action buttons
builder.extend(new ActionsNotificationExtender(getContext(), message, notificationId));
return builder.build();
}
@Override
public int getNextId(PushMessage pushMessage) {
return NotificationIdGenerator.nextID();
}
@Override
public boolean requiresLongRunningTask(PushMessage message) {
return false;
}
}