【问题标题】:Getting channel = null when receiving push notification from urbanairship android收到来自urbanairship android的推送通知时获取channel = null
【发布时间】:2018-11-14 21:42:07
【问题描述】:

当我向我的设备发送推送通知时,我收到可怕的“开发者警告包“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/system_process E/NotificationService: 找不到频道 pkg=com.mycompany.applicationame,channelId=null,id=1007,tag=null, opPkg=com.mycompany.applicationame,callingUid=10081,userId=0, incomingUserId=0, 通知

通知显示,但我收到此错误消息。 UrbanAirship 的新手。我看不出我做错了什么。

【问题讨论】:

    标签: android push-notification urbanairship.com


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多