【问题标题】:Android Studio: Notification not showing even though it is calledAndroid Studio:即使被调用,通知也不会显示
【发布时间】:2020-08-25 13:10:26
【问题描述】:

所以我有一个应用程序,我想每天早上 6 点(午夜)向我发送通知,我在尝试警报后正在与 WorkManager 合作。我目前有一个扩展 Worker 的类,如下所示:

public class NotificationWorker extends Worker {
    Context context;

    public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
        this.context = context;
    }

    @NonNull
    @Override
    public Result doWork() {
        Intent resultIntent = new Intent(context, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);

        Notification.Builder notification = new Notification.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentTitle("App Title")
                .setContentText("Some Text...")
                .setContentIntent(resultPendingIntent);

        NotificationManager notificationManager = (NotificationManager) HomePage.getHomePage().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification.build());

        return Result.success();
    }
}

它是由我的 Activity 的 onCreate() 函数中的这个 sn-p 代码调用的

WorkRequest notificationRequest = new OneTimeWorkRequest.Builder(NotificationWorker.class).build();
        WorkManager.getInstance(this).enqueue(notificationRequest);

doWork() 方法被调用,但没有显示通知。任何帮助将不胜感激。

  • 此代码“应该”仅在加载 Activity 时显示通知,有什么想法可以让它每天重复吗? (最好也使用 WorkManager)

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    从 Android 26 开始,通知必须通过适当的通知通道。

    注意:如果您以 Android 8.0(API 级别 26)为目标并发布 未指定通知渠道的通知,则 通知未出现,系统记录错误。

    有关信息,请参阅以下docs

    您可以使用以下代码来构建频道,其中 CHANNEL_ID 是您选择的字符串

    private void createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = ctx.getString(R.string.my_channel_name);
                String description = ctx.getString(R.string.my_channel_description);
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
                channel.setDescription(description);
                //Register Channel
                NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
    
            }
        }
    

    然后在初始化通知生成器时,为通道 ID 传递一个附加参数

    Notification.Builder notification = new Notification.Builder(context, CHANNEL_ID)
    

    您通常还希望将优先级传递给您的构建器,如下所示,以支持 Android 25 及更低版本

    notification.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    

    【讨论】:

    • 在弄乱了它之后,效果很好!但是:我必须将 Notification.Builder 放在一个 if 语句体中,该语句体检查版本,因为具有 Channel_ID 的构造函数在 26 以下不受支持。else 体包含 setPriority 方法,因为该方法在 26 后已弃用。
    • 我很高兴它成功了。您对 API 检查是正确的,我没有发现这一点,因为我通常使用 NotificationCompat.builder,它提供向后兼容性,并允许您在不破坏任何内容的情况下进行这些调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 2021-08-14
    • 2014-04-21
    • 1970-01-01
    相关资源
    最近更新 更多