android - GCMIntentService 的 Intent Extras 未通过

标签 android android-intent push-notification android-manifest

我编写了GCMIntentService但是调用了Activity无法获取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);
}

调用MainActivity的代码如下。

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

    final Intent receivedIntent = getIntent();

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

我在 receivedIntent 下了断点,里面的 mExtras 显示为 null。

此外,我调用的 MainActivity list 编码如下。

        <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>

我将调用的 Intent 更改为另一个 Activity,它成功加载了 Extras。 替代 Activity list (成功包含 Extras)如下。

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

我做错了什么?

最佳答案

看起来您需要覆盖 onNewIntent(Intent intent) 方法并在 onNewIntent() 内部调用 setIntent(intent)。然后 onResume() 将获取新的 Intent 。参见 onNewIntent()了解更多详情。

关于android - GCMIntentService 的 Intent Extras 未通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17061190/

相关文章:

android - 在 Android Studio 中更新外部库

android - 如何将此数据转换为 JSON 格式并将此数据发布到服务器?

Android:如何在 ActionBarSherlock 选项卡中加载 Activity ?

ios - UIApplication.registerForRemoteNotifications() 只能从主线程调用

ios - 即使应用程序快速关闭,如何处理推送通知

android - 同一个服务器代码能否同时向 APNS 和 GCM 发送通知

android - 在 Lollipop 设备中,我的 Material Design ActionBar 或 StatusBar 上没有显示颜色,但在 Pre-Lollipop 设备的 ActionBar 中显示。为什么?

android - 自定义共享 Intent 的 UI

java - 将 Android Activity 从堆栈移至前端时,如何刷新(重新初始化)它们?所以要再次运行 onCreate 等

java - MediaStore.Images.Media.getBitmap 意外失败