Android notification

Asked

Viewed 222 times

4

I have a very simple project to test that at the event onCreate from screen 1 sends a notification, which by clicking opens screen 2.

Purposely, I quickly click on the notification to open the new screen (in order to test) doing a kind of "Stress" to simulate some kind of error.

The problem is that time opens normally and other times there is a white screen.

Why does this happen?

I tested on 3 devices:

  • Samsung CORE 2 - DIDN’T WORK EVERY TIME.
  • Samsung Galaxy S3 mini - WORKED ALL THE TIME.
  • Samsung Galaxy s duos - GAVE PROBLEM A FEW TIMES

Does this have to do with RAM, processor? Or is my code running in the wrong order?

public void sendNotification() {

    int NOTIFICATION_ID = 1;

    NotificationManagerCompat nm = NotificationManagerCompat.from(this);

    Intent it = new Intent(this, NovaTela.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(NovaTela.class);
    stackBuilder.addNextIntent(it);
    PendingIntent pit = stackBuilder.getPendingIntent(
            0, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setContentIntent(pit)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Teste Notificação")
                    .setContentText("teste");

    nm.notify(NOTIFICATION_ID, mBuilder.build());
}
  • Friend, do a test: add the ACTIVITY_NEW_TASK flag to open the Novatela: Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  • I’ve tried and it’s still the same problem....

  • Displays an error in Logcat?

  • None, so I would like to understand why sometimes appears the white screen where I can click on the elements in the background but I can not see anything, being that in some devices work normally...

1 answer

4

The why appears, time yes, time no, it is a bit complicated! Sometimes it can be the version of the System, among others. Try it this way :

final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());

final Intent i = new Intent(getApplicationContext(), NovaTela.class);

i.setFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP );

builder.setTicker("Ticker text. . . ");
builder.setContentTitle(" Content title text....");
builder.setContentText("Description text....");
builder .setSmallIcon(android.R.drawable.ic_input_add);
builder.setAutoCancel(true);

final PendingIntent open = PendingIntent.getActivity(this, 12345, i, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(open);
notificationManager.notify(12345, builder.build());
  • With this I can add an Addparentstack without having problems?

  • Instead of Addparentstack, you add an Intent it = new Intent(this, Novatela.class);

  • I say because if I have to open this notification without opening the application and pressing the back button, want to go back to screen 1.

  • sorry, I don’t understand... The Service sent the Notification, I click and open the Novatela, if I click Back, it should open the Home screen of the application?

  • Yes! Exactly.

  • In that case you would have trouble using addParentStack?

  • 1

    I believe there is no problem! I will search right, but I think this can be stated in Manifest. In Novatela’s statement, it says that it is an internal screen of the main.

  • Yes this is quiet to do, I wonder if you will not again have the problem of the white screen....

  • Just one more thing...

  • If I’m already on screen 2 and click on the notification is it possible to make her create again? I want this.

  • Without that preserve understands?

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.