user2638180
user2638180

Reputation: 1023

Android notification may open twice (or even more) the app

My app sets a notification with the following code:

private void defineAndLaunchNotification(String apppack,String title, String ContentText)
    {
Context context = getApplicationContext();
PackageManager pm = context.getPackageManager();
Intent LaunchIntent = null;
String name=null;

try {
    if (pm != null)
    {

        ApplicationInfo app = context.getPackageManager().getApplicationInfo(apppack, 0);
        name = (String) pm.getApplicationLabel(app);
        LaunchIntent = pm.getLaunchIntentForPackage(apppack);
    }
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();

}

Intent intent = LaunchIntent; 

if (ContentText.equals("filesystemfullnotification"))
{
    intent.putExtra("start", "fullsystem");
}
else
{
    intent.putExtra("start","incorrecttime");
}

PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);


NotificationCompat.Builder builder=null;
NotificationChannel notificationChannel=null;
int NOTIFICATION_ID = 12345;
if (Build.VERSION.SDK_INT<26) {
    builder =
            new NotificationCompat.Builder(getBaseContext())
                    .setSmallIcon(R.drawable.notification_icon)
                    .setAutoCancel(true)

                    .setContentTitle("title
)


 .setContentText("Content Text");


}
else
{
    int importance=NotificationManager.IMPORTANCE_HIGH;
    notificationChannel=new NotificationChannel("mychannel", "channel", importance);

    builder =
            new NotificationCompat.Builder(getBaseContext(),"mychannel")
                    .setSmallIcon(R.drawable.notification_icon)
                    .setAutoCancel(true)
                    //.setStyle(new NotificationCompat.BigTextStyle().bigText(StaticMethods.giveStringAccordingtoLanguage(title,language)))
                    .setContentTitle(StaticMethods.giveStringAccordingtoLanguage(title, language))
                    .setContentText(StaticMethods.giveStringAccordingtoLanguage(ContentText, language));



}

builder.addAction(R.drawable.notification_icon, "OK", pIntent);


Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
builder.setVibrate(new long[]{0, 1000, 1000, 1000, 1000});


builder.setContentIntent(pIntent);


NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT>=26) {
    nManager.createNotificationChannel(notificationChannel);
}

nManager.notify( (int) ((new Date().getTime() +Math.round(Math.random()*5000) / 1000L) % Integer.MAX_VALUE), builder.build());
}

That code sucessfully shows a notification whenever is called, but problem is that if the notification is tapped twice (or more) times it will open as many instances of my application as that number of times.

This happens even though I have defined in my AndroidManifest.xml my application tag with android:launchMode="singleInstance".

What could I do so the notificacion just reacts to the first tap or only one instance of the app appears?

Upvotes: 0

Views: 1143

Answers (1)

Thadeus Ajayi
Thadeus Ajayi

Reputation: 1283

The way you have build the intent you passed into your pending intent could be the problem. Consider building your intent this way:

    Intent intent = new Intent(context.getApplicationContext(), <Activity to launch>.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra("start", "fullsystem");

By your code you're actually calling to launch the whole application that is why its creating several instances of the application.

You are supposed to launch a particular part of your application, an entry activity to your application.

Upvotes: 2

Related Questions