android java通知未显示

vlju58qv  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(520)

这是我第一次处理通知,但我似乎无法解决这个问题。
我想做的是:
简言之,我有一个服务,可以检查firebase文档中的变量,如果是真的,则显示一个通知。在你说之前,我不能在这个场景中使用云函数。我会详细解释的,请耐心听我说,谢谢。
发生了什么事
根据我的日志,我对正在发生的事情有了一个想法,让我用几点来总结一下。
作业计划程序每30分钟运行一次服务。这是“作业已启动”日志。
它调用checkbookings函数来检查上述变量,即“checkbookings”、“true”和“false”日志。
最后一个日志是“添加通知”,它位于我构建通知的addNotification函数中。此日志显示在“真”日志之后。
问题
已调用添加通知,但未显示该通知。

我会将日志、相关代码粘贴到下面,如有任何意见或建议,将不胜感激,谢谢
日志

  1. D/Notification Service: Job started
  2. D/Notification Service: Check Bookings
  3. D/Notification Service: Job finished
  4. D/Notification Service: true
  5. D/Notification Service: Check Bookings On Success
  6. D/Notification Service: Add Notification
  7. D/Notification Service: Job started
  8. D/Notification Service: Check Bookings
  9. D/Notification Service: false
  10. D/Notification Service: Job finished
  11. D/Notification Service: Job started
  12. D/Notification Service: Check Bookings
  13. D/Notification Service: true
  14. Check Bookings On Success
  15. Add Notification
  16. D/Notification Service: Job finished
  17. W/ample.rehnseha: Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed)
  18. D/Notification Service: Job started
  19. D/Notification Service: Check Bookings
  20. D/Notification Service: false
  21. D/Notification Service: Job finished

代码

  1. private void doBackgroundWork(final JobParameters params) {
  2. new Thread(new Runnable() {
  3. @Override
  4. public void run() {
  5. checkBookings();
  6. if (jobCancelled) {
  7. return;
  8. }
  9. try {
  10. Thread.sleep(1000);
  11. } catch (InterruptedException e) {
  12. e.printStackTrace();
  13. }
  14. Log.d(TAG, "Job finished");
  15. jobFinished(params, true);
  16. }
  17. }).start();
  18. }
  19. public void checkBookings(){
  20. String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
  21. Log.d(TAG,"Check Bookings");
  22. FirebaseFirestore.getInstance().collection("users").document(userId).get()
  23. .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
  24. @Override
  25. public void onSuccess(DocumentSnapshot documentSnapshot) {
  26. Log.d(TAG,""+documentSnapshot.get("notification"));
  27. if ((boolean) documentSnapshot.get("notification")){
  28. Log.d(TAG,"Check Bookings On Success");
  29. addNotification();
  30. }
  31. }
  32. });
  33. }
  34. private void addNotification() {
  35. Log.d(TAG,"Add Notification");
  36. NotificationCompat.Builder builder =
  37. new NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
  38. .setSmallIcon(R.drawable.ic_launcher_foreground)
  39. .setContentTitle("New Booking!")
  40. .setAutoCancel(true)
  41. .setContentText("One of your properties was booked recently, Respond Now!");
  42. Intent notificationIntent = new Intent(this, AddProperty.class);
  43. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
  44. PendingIntent.FLAG_UPDATE_CURRENT);
  45. builder.setContentIntent(contentIntent);
  46. // Add as notification
  47. NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  48. manager.notify(0, builder.build());
  49. FirebaseFirestore.getInstance().collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid())
  50. .update("notification",false);
  51. }

如果你需要更多的细节,请告诉我

i7uq4tfw

i7uq4tfw1#

当您想要创建通知时,首先需要为其创建一个通道。以下是一个例子:

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

在此之后,您可以调用添加通知:

  1. private void addNotification() {
  2. Log.d(TAG,"Add Notification");
  3. NotificationCompat.Builder builder =
  4. new NotificationCompat.Builder(this, CHANNEL_ID)
  5. .setSmallIcon(R.drawable.ic_launcher_foreground)
  6. .setContentTitle("New Booking!")
  7. .setAutoCancel(true)
  8. .setContentText("One of your properties was booked recently, Respond Now!");
  9. Intent notificationIntent = new Intent(this, AddProperty.class);
  10. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
  11. PendingIntent.FLAG_UPDATE_CURRENT);
  12. builder.setContentIntent(contentIntent);
  13. // Add as notification
  14. NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  15. manager.notify(0, builder.build());
  16. FirebaseFirestore.getInstance().collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid())
  17. .update("notification",false);
  18. }

这应该行得通

展开查看全部

相关问题