Intent Extras of GCMIntentService is not passed

2019-05-10 07:24发布

I coded GCMIntentService but called Activity cannot get the Extras.

@Override
protected void onMessage(Context context, Intent intent) {

    Intent resultIntent = new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
        resultIntent.putExtra("PushType", "test"); // Put String Extra.

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

and called MainActivity is coded as below.

@Override
protected void onResume() {
    super.onResume();

    final Intent receivedIntent = getIntent();

    if (receivedIntent != null && receivedIntent.hasExtra("PushType")) {
            // Breakpoint at here. but no Extras are given.
        }       
    }
}

I made breakpoint at the receivedIntent and mExtras inside of it shows null.

Additionally, my called MainActivity manifest is coded as below.

        <activity
        android:name=".MainActivity"
        android:theme="@android:style/Theme.NoTitleBar"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/app_name">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

I changed called intent to another Activity and it successfully loads Extras. The manifest of substitute activity (which successfully contains Extras) was as below.

        <activity
        android:name=".NotificationActivity"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity"/>
    </activity>

What did I wrong?

1条回答
唯我独甜
2楼-- · 2019-05-10 08:02

It looks like you need to override onNewIntent(Intent intent) method and inside onNewIntent() call setIntent(intent). Then onResume() will pick up the new intent. See onNewIntent() for more detail.

查看更多
登录 后发表回答