意向演员与发送的PendingIntent为GCM时丢失(Intent extras being l

2019-10-21 22:56发布

我创建一个notification但使用发送时,它PendingIntentextras得到在这个过程中丢失。 我使用的是singleTop Activity ,所以我在设置一个断点onNewIntent也是我handleIntent ,看看哪一个是射击,当我点击了通知。

当我点击该通知,它解雇了我handleIntent方法。 我想到了一个singleTop活动将火onNewIntent方法?

为什么我的Intent extras创建时丢失在这个过程中notification ? 我调试的intent ,它显示了Bundle[mParcelledData.dataSize=168] 如何unparcel的包?

处理该意向活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    mPrefs = new SharedPrefs(this);
    mDb = DatabaseManager.getInstance(this);
    mVolleyManager = new VolleyManager(this);
    fm = getSupportFragmentManager();

    setupToolbar();
    setupNavDrawer();
    setupBroadcastReceivers();
    setupUniversalImageLoader();

    if (savedInstanceState == null) {
        FragmentTransaction ft = fm.beginTransaction();
        ft = ViewManager.setFragmentAnimations(ft);

        if (!mPrefs.getDemoMode()) {
            if (!mPrefs.getLoggedIn()) {
                ft.add(R.id.container, LoginFragment.newInstance(), GlobalVars
                        .TAG_LOGIN_FRAGMENT).commit();
            }
            else {
                ft.add(R.id.container, BrowseFragment.newInstance(), GlobalVars.
                        TAG_BROWSE_FRAGMENT).commit();
            }
        }
    }

    // Handles intents mainly sent from GCM notification click events
    handleIntent();
}

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    handleIntent();
}

private void handleIntent() {
    Intent intent = getIntent();

    if (intent.hasExtra(GlobalVars.KEY_EVENT)) {
        handleGcmIntent(intent);
    }
}

添加额外的意图和创建通知

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(GlobalVars.KEY_EVENT, events);
        intent.putExtra(GlobalVars.KEY_GAME_DATE_ID, gameDate.getId());

        String text = getResources().getString(R.string.msg_found_match);
        createNotification(text, intent);

private void createNotification(String text, Intent intent) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context
            .NOTIFICATION_SERVICE);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("LovelUp")
            .setContentText(text)
            .setAutoCancel(true)
            .setLights(R.color.primary_blue, 2000, 2000);

    if (mPrefs.getNotificationVibrate()) {
        mBuilder.setVibrate(new long[] {1000, 1000, 1000});
    }
    if (mPrefs.getNotificationSound()) {
        mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    }

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(intent);

    PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent
            .FLAG_CANCEL_CURRENT);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

Answer 1:

您可能需要使用putStringArrayListExtra到ArrayList中添加到包。

你的情况:

intent.putStringArrayListExtra(GlobalVars.KEY_EVENT, events);

然后用getStringArrayListExtra检索数据。

更多信息这里 (搜索putStringArrayListExtra



Answer 2:

这是因为Intent.putExtra(String key, <Whatever> value)不接受的ArrayList作为值。 您的代码去过去的编译器,因为ArrayList的是序列化的,这是公认的,但与其他ArrayList的东西是错误的。

您应该使用[putStringArrayListExtra()][1]



文章来源: Intent extras being lost when sending with PendingIntent for GCM