代码之家  ›  专栏  ›  技术社区  ›  Kristy Welsh

从urbanairship android接收推送通知时获取通道=null

  •  0
  • Kristy Welsh  · 技术社区  · 6 年前

    当我向我的设备发送推送通知时,我收到了可怕的“开发者警告包com.mycompany.applicationame”未能在“null”频道上发布通知”Toast消息。我正在运行Android API 27。这是我的代码:

    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/系统流程 E/NotificationService:找不到的频道 pkg=com。我的公司。applicationame,channelId=null,id=1007,tag=null, opPkg=com。我的公司。applicationame,callingUid=10081,userId=0,

    通知会显示,但我收到了这个错误消息。城市航空公司的新成员。我看不出我做错了什么。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Kristy Welsh    6 年前

    问题是我没有正确设置通知生成器。

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