• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Show notification when App is closed

DJxLaurens

Lurker
Hello Everyone!

On my internship I am programming a Android app.
I have a basic knowledge of programming Android.

Now I have a question.
In my code I use the Notification Manager for show notifications.

This is the code:
public void showNotification(String numberOfMessages){
if(Integer.parseInt(numberOfMessages.toString().trim()) == 0) {
hideNotification();
}else{
Intent intent = new Intent(this, MainActivity.class);
int requestID = (int) System.currentTimeMillis();
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, intent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Notification notification = builder.setContentIntent(contentIntent)
.setTicker("New message")
.setContentTitle("You have received " + numberOfMessages + "new messages")
.setContentText("Tap to open the message center.")
.setSmallIcon(R.mipmap.ic_launcher)
.build();

notification.flags = Notification.FLAG_AUTO_CANCEL;

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}

When I close the app, the notifications doesn't show. How can I code that the notification shows when the App is closed?
I hope anyone can help me!
 
Back
Top Bottom