filipebarretto filipebarretto - 24 days ago 9
Android Question

How to stop Push Notification from automatically opening app?

I have an Android App with Push Notifications. I'm having a problem with some devices that when I send a push notification, the user's app opens automatically. I don't want to do that. I want the notification to show normally and if the users clicks on it, the app opens.

How do I prevent the app from opening automatically?

Here is the code from my intent for push notifications:

Intent notificationIntent = new Intent(this, MainActivity.class);

notificationIntent.setFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

// Display a notification with an icon, message as content, and default sound. It also
// opens the app when the notification is clicked.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(message)
.setColor(0xFF221E80)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setFullScreenIntent(contentIntent, true)
.setContentIntent(contentIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);

notificationManager.notify(0, builder.build());

Answer

If you take a look at the documentation for the NotificationCompat.Builder you can see the setFullScreenIntent is the issue.

Documentation on that call:

An intent to launch instead of posting the notification to the status bar. Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time. If this facility is used for something else, please give the user an option to turn it off and use a normal notification, as this can be extremely disruptive.

On some platforms, the system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device.

You should remove setFullScreenIntent from your builder.

Comments