【问题标题】:Scheduling Notifications not working on some android devices of version 8 and 9计划通知不适用于版本 8 和 9 的某些 android 设备
【发布时间】:2019-10-08 07:30:55
【问题描述】:

通知在某些版本 8 的真实 android 设备和 9 即使启用了通知设置,但工作正常 模拟器。我正在安排警报管理器以触发通知 那个时候使用一个广播接收器,其中通知是 生成。

private void generateNotification(Context context, String message) {

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        createNotificationChannel(notificationManager);
        RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.notification_main);
        view.setImageViewResource(R.id.ImageView1, R.drawable.tenkey_icon);
        view.setTextViewText(R.id.title1, Utils.NOTIFICATION_TITLE);
        view.setTextViewText(R.id.message1, message);
        view.setTextViewText(R.id.time1, notificationTiming);
        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.putExtra("content", message);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        Notification.Builder builder=new Notification.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setOngoing(true)
                .setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, 0))
                .setContent(view);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID);
        }
        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify((int)System.currentTimeMillis() % 1000000, notification);
    }



private void createNotificationChannel(NotificationManager manager) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Shankaram",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            manager.createNotificationChannel(serviceChannel);
        }
    }

在模拟器中它工作正常,我没有所有真实设备 测试和调试我卡在这一点上。任何事情都应该处理 android pie上的警报管理器?请推荐

【问题讨论】:

    标签: android notifications version


    【解决方案1】:

    在 Android 8 及更高版本中,您必须使用 Notification channels

    从 Android 8.0(API 级别 26)开始,所有通知都必须分配给一个频道,否则它不会出现。通过将通知分类为频道,用户可以为您的应用禁用特定的通知频道(而不是禁用所有通知),并且用户可以控制每个频道的视觉和听觉选项——所有这些都来自 Android 系统设置(图 11)。用户还可以长按通知来更改关联频道的行为。

    【讨论】:

    • 我已经设置了这样的频道 ID if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setChannelId(CHANNEL_ID); }
    • 您是否尝试在createNotificationChannel 中添加此行NotificationManager manager = getSystemService(NotificationManager.class);
    • 是的,我试过但没有用。主要是它适用于模拟器和少数android 9。并且很少有它不起作用。例如:三星 Galaxy SM A105F android 9、realme pro android 9 不工作。
    【解决方案2】:

    我已经通过深入检查所有通知解决了这个问题 手机中的设置并启用所有设置,最后 工作。没有进行任何代码更改。

    【讨论】:

      猜你喜欢
      • 2023-02-05
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 2016-10-11
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多