【问题标题】:Unable to receive local notifications Android无法接收本地通知 Android
【发布时间】:2018-11-17 11:07:27
【问题描述】:

我是 Android 编程新手,我正在尝试构建我的第一个应用程序。现在我想发送本地通知。不幸的是,我无法在运行 API 28 及更高版本的设备上收到通知。我知道自 Oreo 以来发送通知的方式发生了变化,并且我已经包含了创建通道的代码。似乎如果我在具有较低 API(例如 19)的模拟器上运行该应用程序,则会收到通知。此外,如果我将代码复制到一个新项目中,我也会收到通知,即使在运行 Android Oreo 的模拟器上也是如此。项目中的哪些设置可能导致在 Android Oreo 上收不到通知? (Logcat中没有错误)

我发送通知的代码:

public static final String CHANNEL_1_ID = "channel1";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    createNotificationChannels();
}

public void setNotif(View view) {
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("hey")
            .setContentText("world")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .build();
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(1, notification);
}

private void createNotificationChannels() {
    // if higher than android oreo
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel channel1 = new NotificationChannel(CHANNEL_1_ID, "Channel 1", NotificationManager.IMPORTANCE_HIGH);
        channel1.setDescription("This is channel 1");

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel1);
    }
}

通过点击按钮调用 setNotif 方法。同样,我是 Android 编程的新手,所以任何建议都会有所帮助。甚至可能是我可以诊断问题的不同方式。谢谢!

【问题讨论】:

    标签: java android android-studio notifications


    【解决方案1】:

    在您的 setNotif() 方法中添加此检查:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)  
      notificationManager.createNotificationChannel(getNotificationChannel(context));
    

    setNotif() 方法会变成这样:

    public void setNotif(View view) {
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("hey")
                .setContentText("world")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .build();
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)        notificationManager.createNotificationChannel(getNotificationChannel(context));
        notificationManager.notify(1, notification);
    }
    

    getNotificationChannel() 方法如下所示:

    private static NotificationChannel getNotificationChannel(Context context) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
                return null;
            CharSequence name = context.getString(R.string.app_name);// The user-visible name of the channel.
            int importance = android.app.NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(BuildConfig.APPLICATION_ID, name, importance);
            notificationChannel.setShowBadge(true);
            return notificationChannel;
        }
    

    【讨论】:

    • notificationManager 没有 createNotificationChannel 方法,我错过了什么吗?
    • 您可以尝试获取 NottificationManager 服务,而不是 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); android.app.NotificationManager notificationManager = (android.app.NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    • 它仍然不起作用:(我认为疯狂的是,如果我在通知部分进入模拟器(或设备)的设置,我可以在最近发送的窗格中看到我的应用程序...此外,如果我点击应用程序,我可以看到我设置的通知渠道。我不认为渠道是问题...其他的是
    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多