14

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

任何帮助,将不胜感激!谢谢你们。

4

3 回答 3

13

好的,在发布我的问题后很快就可以使用了。我认为我们代码的主要区别在于我将标志“PendingIntent.FLAG_UPDATE_CURRENT”传递给 PendingIntent 对象的创建/检索。这篇文章帮助我弄清楚了这一点。

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());
于 2015-07-16T03:42:03.410 回答
13

首先,您应该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(.. ) 当前活动的方法。

于 2017-08-25T16:30:34.817 回答
0

经过长时间的检查,我注意到如果您在 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 可能是更好的选择(未更新数据没有问题)

于 2017-07-10T14:10:52.350 回答