【问题标题】:Notification not showing up on Android 8通知未在 Android 8 上显示
【发布时间】:2018-12-23 17:03:59
【问题描述】:

我已阅读有关此问题的大多数问题/答案,但我的通知仍未显示。

我的广播接收器被调用,但没有任何反应。没有错误消息也没有异常。这是我的代码:

public class AlarmReceiver extends BroadcastReceiver {

    private static int notificationId = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 101, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
                context, "mychannel")
                .setSmallIcon(R.drawable.icon_awake)
                .setContentTitle("MyAlarm")
                .setContentText("Something")
                .setAutoCancel(false)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent);
        notificationManager.notify(notificationId++, mNotifyBuilder.build());
    }

}

【问题讨论】:

    标签: android android-intent notifications android-8.0-oreo


    【解决方案1】:

    我认为您忘记在 Oreo 8.0 上阅读 Google 文档。 如果您想在 8.0 上创建通知,您需要使用 NotificationChannel

    像这样:

        NotificationChannel channel = null;
        String chanel = "MY_Musixbox";
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_LOW;
            channel = new NotificationChannel(chanel, "MusicBox", importance);
            notificationManager.createNotificationChannel(channel);
    
        }
    

    然后添加您的通知

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
                    context, "mychannel")
                    .setSmallIcon(R.drawable.icon_awake)
                    .setContentTitle("MyAlarm")
                    .setContentText("Something")
                     .setChannelId(chanel);
                    .setAutoCancel(false)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent);
            notificationManager.notify(notificationId++, mNotifyBuilder.build());
    

    【讨论】:

      【解决方案2】:

      好的,我会自己回答,以防有人发现:看来您也需要自己手动创建通知通道:

      if (Build.VERSION.SDK_INT >= 26) {
                  NotificationChannel channel = new NotificationChannel("mychannel",
                          "Channel name",
                          NotificationManager.IMPORTANCE_HIGH);
                  channel.setDescription("Channel description");
                  notificationManager.createNotificationChannel(channel);
              }
      

      【讨论】:

        【解决方案3】:
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Intent notificationIntent = new Intent(context, MainActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 101, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
                    context, "mychannel")
                    .setSmallIcon(R.drawable.icon_awake)
                    .setContentTitle("MyAlarm")
                    .setContentText("Something")
                    .setAutoCancel(false)
                    .setWhen(System.currentTimeMillis())
                    .setChannelId("default_channel")
                    .setContentIntent(pendingIntent);
            notificationManager.notify(notificationId++, mNotifyBuilder.build());
        

        试试这个。您必须定义频道 ID。这在 android 8.0 中是强制性的

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-02-23
          • 1970-01-01
          • 1970-01-01
          • 2013-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多