0

I am actually working with notifications in Android.

To do my application test, I follow the guide on android.developer .

I create my notification, it works well. When I click on it, it removes the notification from the notification tab and after I go to my Activity.

Here is the code I use to do that :

    public void createSimpleNotification() {
    NotificationCompat.Builder  mNotifBuilder = new NotificationCompat.Builder(this.getApplicationContext())
    .setSmallIcon(R.drawable.ic_notif).setContentTitle("Simple notif").setContentText("Welcome on our app, click here");

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

    TaskStackBuilder mStackBuilder = TaskStackBuilder.create(this.getApplicationContext());
    mStackBuilder.addParentStack(MainActivity.class);
    mStackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendingIntent = mStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    mNotifBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifManager.notify(0, mNotifBuilder.build());
}

I only have my MainActivity and a Button. When I click on the button, the notification appears and I click on notification, i come back to my MainActivity.

Here is my problem :

I want to update my MainActivity UI after clicking on the notification. After clicking on notification, I need to create a Dialog who shows the text of the notification.

How can I modify my Activity and add new View after clicking on a notification?

ahmed_khan_89
  • 2,755
  • 26
  • 49
Androzr
  • 65
  • 1
  • 10

2 Answers2

0

Use a view that is already there. So you create it in your activity, and set it's Visibility to FALSE.

Now if you want it to appear, put your notification text in it and set Visibility to TRUE. This should serve your request ;)

PKlumpp
  • 4,913
  • 8
  • 36
  • 64
  • I aggree with you but how I access my View on onClick on the notification ? – Androzr Jun 11 '14 at 08:00
  • You create your View in your Activity. Just make it a static class variable. then you can access it via `ActivityWithView.myNotificationView` for example – PKlumpp Jun 11 '14 at 08:03
  • Yes I understand what you say but how I know if the notification has been clicked ? – Androzr Jun 11 '14 at 08:05
  • `After clicking on notification, I need to create a Dialog` I assume that there is a Button or something else. Just add an onClickListener – PKlumpp Jun 11 '14 at 08:08
  • Yes, there is a Button, when I click on it the notification appears in notification tab. But if I had my View after the onClickListener of the Button, it will display directly the View. I will not have the time to click on the notification. The click on the notification must change the Activity view. – Androzr Jun 11 '14 at 08:12
  • What you need for this is a PendingIntent: [link](http://developer.android.com/reference/android/app/PendingIntent.html). Here is an example on how to use it with notifications: -[link](http://www.vogella.com/tutorials/AndroidNotifications/article.html#pendingintent) – PKlumpp Jun 11 '14 at 08:21
  • I have only one Activity. I can't create an other one – Androzr Jun 11 '14 at 08:24
  • This will help you: [link](http://stackoverflow.com/questions/13822509/android-call-method-on-notification-click) – PKlumpp Jun 11 '14 at 08:29
  • I will try with links you shared. I come back if it doesn't work and accept the answer if it works. – Androzr Jun 11 '14 at 08:36
  • Thank you for your help and your time ! It works with the `onNewIntent` method and your links. – Androzr Jun 11 '14 at 08:49
0

you can add put an Extra to your result Intent

such as:

resultIntent.putExtra(EXTRA_NAME, EXTRA_VALUE);

then handle it in your MainActivity onNewIntent

    protected void onNewIntent(Intent intent) {
        if (intent.hasExtra(EXTRA_NAME)) {
            // do your things here
        }
    }
Vic Ren
  • 1
  • 2