簡體   English   中英

從通知中調用onNewIntent()

[英]Calling onNewIntent() from a notification

MainActivity托管我的所有片段。

當用戶點擊從Google Cloud Messaging收到的通知時,我需要顯示一個特定的片段。

我的印象是我可以從onNewIntent()處理這個問題,但是在我按下通知后這個方法似乎沒有觸發。

這是我的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());

}

在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();

    }

}

知道為什么onNewIntent()沒有被調用?

嘗試將launchMode of your activity as "singleTop"設置launchMode of your activity as "singleTop"清單中的launchMode of your activity as "singleTop"

來自官方文件。

這被稱為在其包中將launchMode設置為“singleTop”的活動,或者在調用startActivity(Intent)時客戶端使用FLAG_ACTIVITY_SINGLE_TOP標志。 在任何一種情況下,當在活動堆棧的頂部而不是正在啟動的活動的新實例重新啟動活動時,將在現有實例上使用用於重新啟動的Intent調用onNewIntent()它。

在接收新意圖之前,活動將始終暫停,因此您可以指望在此方法之后調用onResume()。

請注意,getIntent()仍然返回原始Intent。 您可以使用setIntent(Intent)將其更新為此新Intent。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM