【问题标题】:Android OnNewIntent not called未调用 Android OnNewIntent
【发布时间】:2015-06-08 11:52:01
【问题描述】:

我看到了几种方法,我尝试了所有方法,但无法使其发挥作用。我不知道为什么它如此复杂,在文档中看起来如此简单!我想通过通知触发 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());
}

这是我的 MainMenuActivity 中的 OnNewIntent 方法:

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

    ...
}

我从来没有接到 OnNewIntent 电话。我不知道我做错了什么。我在整个应用程序中只使用了 2 个活动,并且 MainMenuActivity 位于 LoginActivity 之后,因此 MainMenuActivity 应该始终位于堆栈顶部(我在 MainMenuActivity 中替换了更多片段)。

任何帮助将不胜感激!谢谢各位。

【问题讨论】:

  • 你有没有得到任何地方?我的情况几乎相同。

标签: android notifications push-notification onnewintent


【解决方案1】:

好的,在发布我的问题后很快就可以使用了。我认为我们代码的主要区别在于我将标志“PendingIntent.FLAG_UPDATE_CURRENT”传递给 PendingIntent 对象的创建/检索。这个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());

【讨论】:

  • 您还添加了“alertType”值 - 这是解决我同样问题的关键!谢谢!
【解决方案2】:

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

<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的方法。

【讨论】:

  • 你是最棒的!我花了很长时间才发现你需要添加 Intent.ACTION_MAIN 和 INTENT.CATEGORY_LAUNCHER !!!我还没有看到它写在任何地方......
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多