简体   繁体   English

从通知中调用onNewIntent()

[英]Calling onNewIntent() from a notification

MainActivity hosts all of my fragments. MainActivity托管我的所有片段。

I need to display one particular fragment when the user clicks on a Notification that was received from Google Cloud Messaging . 当用户点击从Google Cloud Messaging收到的通知时,我需要显示一个特定的片段。

I was under the impression that I could handle this from onNewIntent() , but this method doesn't seem to fire after I press the notification. 我的印象是我可以从onNewIntent()处理这个问题,但是在我按下通知后这个方法似乎没有触发。

Here's my send notification method in my GcmIntentService class: 这是我的GcmIntentService类中的发送通知方法:

private void sendNotification(String message, String eventId) {

    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.putExtra(Constants.eventIdBundleKey, eventId);

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

    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_test)
                    .setContentTitle(getString(R.string.app_name))
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(message))
                    .setContentText(message);
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

and here in MainActivity, where I'm trying to receive and process: 在MainActivity中,我正在尝试接收和处理:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);


    if(intent.hasExtra(Constants.eventIdBundleKey)) {

        Fragment fragment = GiftsPriceFragment.newInstance(Common.retrieveAppUser(this), null, null);
        mFm.beginTransaction().replace(R.id.content_frame, fragment)
                .addToBackStack(GiftsPriceFragment.FRAGMENT_TAG).commit();

    }

}

Any idea why onNewIntent() doesn't get called? 知道为什么onNewIntent()没有被调用?

Try setting launchMode of your activity as "singleTop" in manifest. 尝试将launchMode of your activity as "singleTop"设置launchMode of your activity as "singleTop"清单中的launchMode of your activity as "singleTop"

From Official documentation. 来自官方文件。

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). 这被称为在其包中将launchMode设置为“singleTop”的活动,或者在调用startActivity(Intent)时客户端使用FLAG_ACTIVITY_SINGLE_TOP标志。 In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it. 在任何一种情况下,当在活动堆栈的顶部而不是正在启动的活动的新实例重新启动活动时,将在现有实例上使用用于重新启动的Intent调用onNewIntent()它。

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method. 在接收新意图之前,活动将始终暂停,因此您可以指望在此方法之后调用onResume()。

Note that getIntent() still returns the original Intent. 请注意,getIntent()仍然返回原始Intent。 You can use setIntent(Intent) to update it to this new Intent. 您可以使用setIntent(Intent)将其更新为此新Intent。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击通知时,应用程序不启动或调用onNewIntent - Application not launching or calling onNewIntent when notification clicked 通知点击newIntent未调用 - Notification click onNewIntent not called 我的onNewIntent没有打电话 - my onNewIntent is not calling NFC标签检测未调用onNewIntent,而是从主活动启动 - NFC tag detection is not calling onNewIntent and it's Launching From Main Activity 从锁定屏幕点击通知时不会调用 OnNewIntent。 当通知点击锁屏时,意图未通过 - OnNewIntent not called when notification is hit from lock screen. And intent not passed when notification clicked on lockscreen 先前从通知启动应用程序时,Android上的FCM不会触发`onNewIntent` - FCM on Android does not trigger `onNewIntent` when app was previously launched from notification 使用Parse.com推送通知时未调用onnewIntent方法 - onnewIntent method is not called when using Parse.com push notification 为什么从 SearchActivity 调用 onNewIntent 两次? - Why onNewIntent is invoked twice from SearchActivity? 从后台启动应用程序时会调用onNewIntent - onNewIntent is called when app is launched from background Android在onNewIntent中从Camera Intent获得结果 - Android get result from Camera Intent in onNewIntent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM