【问题标题】:Notification is not showing after updating Android Studio更新 Android Studio 后未显示通知
【发布时间】:2019-01-29 04:16:59
【问题描述】:

我最近对 ​​Android Studio 进行了更新,之后我无法显示我的通知。

代码在更新之前最初运行良好,所以我认为问题出在软件而不是代码上。更新后有什么需要更改的吗?

我的程序实际上会显示一条 Toast 消息,并且还会有一条通知说帐户已注册(在注册帐户后)。但是,在 toast 消息之后,另一条 toast 消息显示“开发者警告包”com.example.jianminong.aucon”无法发布通知渠道“个人”请参阅日志以获取更多详细信息。

public void displayNotification(){
    NotificationCompat.Builder builder = new 
NotificationCompat.Builder(this,CHANNEL_ID);
    builder.setSmallIcon(R.drawable.aucon);
    builder.setContentTitle("Welcome " + 
editUsername.getText().toString());
    builder.setContentText("You have just created an account with 
the email of " +editEmail.getText().toString());
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

    NotificationManagerCompat notificationManagerCompat = 
NotificationManagerCompat.from(this);

notificationManagerCompat.notify(NOTIFICATION_ID,builder.build());
}


private void createNotificationChannel(){
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
    {
        CharSequence name = "Personal";
        String description = "HAHA";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;

        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,name,importance);

        notificationChannel.setDescription(description);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

【问题讨论】:

    标签: android android-studio notifications android-notifications


    【解决方案1】:

    您是否在代码createNotificationChannel() 方法中的某个地方调用了应该创建通知通道的方法?因为如果没有,则不会创建通道,因此不会创建 toast 消息。您可以在显示通知之前调用它:

    public void displayNotification(){
        createNotificationChannel()
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID);
        ...
    }
    

    【讨论】:

      【解决方案2】:

      如果您在较新的 android 设备上运行此功能,则需要使用新的通知渠道:

      从 Android 8.0(API 级别 26)开始,所有通知都必须分配给一个频道,否则它不会出现。

      你需要遵循这个: https://developer.android.com/training/notify-user/channels

      【讨论】:

      • 我可以知道我的代码的哪一部分需要修改吗?还是只是模拟器中的一个设置?
      • 在调用displayNotification之前确保你调用的是createNotificationChannel
      • 谢谢。除此之外,ParentActivity 是否也有任何变化?
      • 例如:
      • 感谢采纳答案,我不认为AndroidManifest的变化与通知问题有关,我认为你很好;)
      猜你喜欢
      • 2023-01-31
      • 2016-08-07
      • 2017-07-23
      • 2013-04-09
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多