getIntent() Extras always NULL

前端 未结 5 2147
南方客
南方客 2020-11-27 03:19

I wrote a simple Android App that show a custom Notification like this:

Context context = getApplicationContext();          
NotificationManager manager = (N         


        
相关标签:
5条回答
  • 2020-11-27 03:54

    Look at Shared Preferences for passing and retrieving persistent key/value pairs.

    0 讨论(0)
  • 2020-11-27 03:57

    Since it seems your activity is already running, I think you need to specify FLAG_UPDATE_CURRENT, otherwise the getIntent() call will return the previous one. See this answer.

    0 讨论(0)
  • 2020-11-27 04:09

    This is the scenario:
    The method getIntent() returns the FIRST intent than launch activity.

    So, when the activity is CLOSED (terminated) and the user clicks on the notification, it will run a new instance of the activity and getIntent() works as expected (Extras is not null).

    But if the activity is "sleeping" (it is in the background) and the user clicks on the notification, getIntent() always returns the very FIRST intent that started the activity and NOT the notification intent.

    So to catch the notification intent while the application is running, simply use this

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    and then override onNewIntent(Intent newintent).

    So when an application first runs, getIntent() can be used and when application is resumed from sleeping, onNewIntent works.

    0 讨论(0)
  • 2020-11-27 04:09

    Problem: You are sending the same request code for your pending intens. Change this.

    Solution: Set global variable int UNIQUE_INT_PER_CALL =0 and when you create pendingIntent call like below.

    PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
    UNIQUE_INT_PER_CALL++; // to increment.
    
    0 讨论(0)
  • 2020-11-27 04:18

    Just Write this code above your on top of your Resume() method. This is all it takes. This refreshes intent - I don't really know, but it works.

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }
    
    0 讨论(0)
提交回复
热议问题