Android Java 通知未显示

Android Java Notification Not Displaying

这是我第一次使用通知,我似乎无法弄清楚这个问题。

我想做什么:

简而言之,我有一项服务可以检查 firebase 文档中的变量,如果是,则显示通知。在你说之前,我不能在这种情况下使用 Cloud Functions。我会详细解释的,所以请耐心等待,谢谢。

发生了什么事

根据我的日志,我知道发生了什么,让我用几个简短的要点来总结一下。

  1. 该服务 运行 每 30 分钟一次,由 Job Scheduler 提供。那是“作业开始”日志。
  2. 它调用检查上述变量的检查预订功能,即“Checkbookings”、“true”和“false”日志。
  3. 最后一个日志是“添加通知”,它位于我构建通知的 addNotifcation 函数中。此日志出现在“真实”日志之后。

问题

已调用添加通知,但未显示通知。

我会粘贴日志,任何输入或建议下面的相关代码将不胜感激,谢谢

日志

D/Notification Service: Job started
D/Notification Service: Check Bookings
D/Notification Service: Job finished
D/Notification Service: true
D/Notification Service: Check Bookings On Success
D/Notification Service: Add Notification
D/Notification Service: Job started
D/Notification Service: Check Bookings
D/Notification Service: false
D/Notification Service: Job finished
D/Notification Service: Job started
D/Notification Service: Check Bookings
D/Notification Service: true
    Check Bookings On Success
    Add Notification
D/Notification Service: Job finished
W/ample.rehnseha: Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed)
D/Notification Service: Job started
D/Notification Service: Check Bookings
D/Notification Service: false
D/Notification Service: Job finished

代码

private void doBackgroundWork(final JobParameters params) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                checkBookings();
                if (jobCancelled) {
                    return;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.d(TAG, "Job finished");
                jobFinished(params, true);
            }
        }).start();
    }

    public void checkBookings(){
        String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
        Log.d(TAG,"Check Bookings");
        FirebaseFirestore.getInstance().collection("users").document(userId).get()
                .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        Log.d(TAG,""+documentSnapshot.get("notification"));
                        if ((boolean) documentSnapshot.get("notification")){
                            Log.d(TAG,"Check Bookings On Success");
                            addNotification();
                        }
                    }
                });
    }

    private void addNotification() {
        Log.d(TAG,"Add Notification");
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setContentTitle("New Booking!")
                        .setAutoCancel(true)
                        .setContentText("One of your properties was booked recently, Respond Now!");

        Intent notificationIntent = new Intent(this, AddProperty.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        // Add as notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
        FirebaseFirestore.getInstance().collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .update("notification",false);
    }

如果您需要更多详细信息,请告诉我

当您要创建通知时,您需要先为其创建一个频道。这是一个例子:

 private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = CHANNEL_NAME;
        String description = CHANNEL_DESC;
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

之后你可以调用添加通知:

  private void addNotification() {
    Log.d(TAG,"Add Notification");
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("New Booking!")
                    .setAutoCancel(true)
                    .setContentText("One of your properties was booked recently, Respond Now!");

    Intent notificationIntent = new Intent(this, AddProperty.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());
    FirebaseFirestore.getInstance().collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .update("notification",false);
}

这应该有效