Android 未调用 OnNewIntent

Android OnNewIntent not called

我看到了几种方法,我尝试了所有方法,但无法实现 work.I 不知道为什么这么复杂,在文档中看起来很简单!我想通过通知触发 OnNewIntent(用户在通知栏中点击它)。

目前我已将 Activity 设置为 singleTop

<activity
    android:name="-.-.-.MainMenuActivity"
    android:launchMode="singleTop"
    android:screenOrientation="portrait" >
</activity>

这是我创建通知的代码:

public void showNotification(String text, String componentId){
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Title")   
            .setAutoCancel(true)
            .setContentText(text);

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainMenuActivity.class);
    if(!componentId.equalsIgnoreCase("")){                         
        resultIntent.putExtra("componentId", componentId);
    }   
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
    mBuilder.setFullScreenIntent(resultPendingIntent, false);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(0, mBuilder.build());
}

这是我的 MainMenu 中的 OnNewIntent 方法Activity:

@Override
protected void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    super.onNewIntent(intent);
    setIntent(intent);

    ...
}

我从来没有接到 OnNewIntent 电话。我不知道我做错了什么。我在整个应用程序中只使用了 2 个活动,并且 MainMenuActivity 在 LoginActivity 之后出现,所以 MainMenuActivity 无论如何应该总是在堆栈的顶部(我有更多的片段我在 MainMenuActivity).

中替换它们

如有任何帮助,我们将不胜感激!谢谢大家。

好的,发布我的问题后很快就可以使用了。我认为我们代码的主要区别在于我将标志 "PendingIntent.FLAG_UPDATE_CURRENT" 传递给 PendingIntent 对象的 creation/retrieval。 post 帮助我解决了这个问题。

Notification.Builder mBuilder =
                new Notification.Builder(context)
                .setSmallIcon(R.drawable.notifyicon)
                .setContentTitle(title)
                .setContentText(extras.getString("message"))
                .setAutoCancel(true)
                .setOnlyAlertOnce(false)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setVibrate(new long[] {0,500,250,500});

        if(badgeCount > 1)
            mBuilder.setNumber(badgeCount);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, SiteViewActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        resultIntent.putExtra(NOTIFY_INTENT_TYPE_KEY, alertType);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(context, alertType, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager notifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notifyMgr.notify(alertType, mBuilder.build());

经过长时间的检查,我注意到 Android 4.4(Kitkat 和可能更低的版本)如果您在 pendingIntent 上使用 FLAG_UPDATE_CURRENT 则不会调用 onNewIntent 和 onResume。将其更改为 FLAG_ONE_SHOT 后,它将开始工作。在 Android 6(可能还有 5)上,但是 onNewIntent 即使使用 FLAG_UPDATE_CURRENT 标志也能工作。

但是,如果您创建多个待定意图(例如,在收到两个通知后),数据将不会使用标志 FLAG_ONE_SHOT 更新,因此标志 FLAG_CANCEL_CURRENT 可能是更好的选择(它没有更新数据的问题)

首先,您应该将 android:launchMode="singleTop" 添加到清单文件中的 activity 定义中,如下所示

<activity
    android:name="MainActivity"
    android:launchMode="singleTop"
</activity>

然后就像 Hasan Masud 说的那样,您应该像下面那样在您的操作中添加 Intent.ACTION_MAINIntent.CATEGORY_LAUNCHER

Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

就是这样。通过这种方式,如果应用程序打开并且当您单击通知时,onNewIntent(..) 方法将触发,但无论发生什么,如果应用程序关闭,当您单击通知时,通知意图将转到 onCreate(.. ) 当前方法 Activity.