这是我第一次处理通知,但我似乎无法解决这个问题。
我想做的是:
简言之,我有一个服务,可以检查firebase文档中的变量,如果是真的,则显示一个通知。在你说之前,我不能在这个场景中使用云函数。我会详细解释的,请耐心听我说,谢谢。
发生了什么事
根据我的日志,我对正在发生的事情有了一个想法,让我用几点来总结一下。
作业计划程序每30分钟运行一次服务。这是“作业已启动”日志。
它调用checkbookings函数来检查上述变量,即“checkbookings”、“true”和“false”日志。
最后一个日志是“添加通知”,它位于我构建通知的addNotification函数中。此日志显示在“真”日志之后。
问题
已调用添加通知,但未显示该通知。
我会将日志、相关代码粘贴到下面,如有任何意见或建议,将不胜感激,谢谢
日志
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);
}
如果你需要更多的细节,请告诉我
1条答案
按热度按时间i7uq4tfw1#
当您想要创建通知时,首先需要为其创建一个通道。以下是一个例子:
在此之后,您可以调用添加通知:
这应该行得通