【发布时间】:2019-07-01 19:55:20
【问题描述】:
我创建了一个应用内通知,该通知由后台服务触发,由 firebase 值事件侦听器触发。
即使应用在后台,手机解锁时也会显示通知,但手机锁定时不会显示。
public int onStartCommand(Intent intent, int flags, int startId) {
flag = true;
queueList = new ArrayList<>();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null) {
reference = FirebaseDatabase.getInstance().getReference("queue").child(firebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
queueList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
try {
ChatQueue chatQueue = snapshot.getValue(ChatQueue.class);
if (chatQueue.getStatus().equals("pending")) {
queueList.add(chatQueue);
}
} catch (NullPointerException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
}
if (count > 0) {
if (queueList.size() > 0) {
notifyDoctor();
ShortcutBadger.applyCount(getApplicationContext(), queueList.size());
} else {
ShortcutBadger.removeCount(getApplicationContext());
}
}
count++;
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
return START_NOT_STICKY;
}
private void sendNotification() {
int j = 220;
Intent intent = new Intent(this, ChatHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent, PendingIntent.FLAG_ONE_SHOT);
final Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notif_icon)
.setContentTitle("Patient Alert!")
.setContentText("You have a new patient.")
.setAutoCancel(true)
.setSound(defaultSound).setContentIntent(pendingIntent);
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
builder.setPriority(Notification.PRIORITY_MAX);
notificationManager.notify(j, builder.build());
}
【问题讨论】:
-
显示您到目前为止所做的代码,以便我们更好地帮助您
-
添加了通知医生我调用通知的代码
-
请添加代码以显示通知
-
刚刚做了,notifydoctor()函数有条件检查版本,因为oreo有不同的通知,调用sendnotifciation()函数
标签: android android-notifications