Reputation: 321
I have created an in app notification that gets fired with a background service, which is triggered by firebase value event listener.
The notification shows when the phone is unlocked even if the app is in background but wont show when phone is locked.
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());
}
Upvotes: 1
Views: 2051
Reputation: 321
I was able to work around the problem, it seemed that android OS would kill services in background for battery saving. AS i was sending notifications from within the app and not the server this was supposed to happen therefore a foreground service was a perfect fit as it would not be killed by the system and hence my problem solved.
Thanks everyone.
Upvotes: 0
Reputation: 6988
Unless you share code, it's unclear which kind of notification you are displaying.
If it's a heads-up notification, this is the expected behaviour:
it appears only if the device is unlocked.
If the device you are testing with runs on Android version below 5.0, it won't show the notification:
Beginning with Android 5.0, notifications can appear on the lock screen.
And even though you can set your notification to be displayed on the lock screen:
You can programmatically set the level of detail visible in notifications posted by your app on a secure lock screen, or even whether the notification will show on the lock screen at all.
You would have to check the settings on the phone, because:
Users can use the system settings to choose the level of detail visible in lock screen notifications, including the option to disable all lock screen notifications. Starting with Android 8.0, users can choose to disable or enable lock screen notifications for each notification channel.
Upvotes: 2