我看到了几种方法,并且尝试了所有方法,但是无法使它起作用。我不知道为什么它如此复杂,在文档中看起来如此简单! 我想用一个通知触发OnNewIntent(用户在通知栏中单击它)。

目前,我已将"活动"设置为singleTop

android:name="-.-.-.MainMenuActivity"

android:launchMode="singleTop"

android:screenOrientation="portrait">

这是我创建通知的代码:

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中将其替换的片段更多)。

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

你有没有得到这个? 我处于几乎相同的情况。

确定,在发布我的问题后很快就可以使用。我认为我们代码中的关键区别在于,我将标志" 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());

您还添加alertType值-这是我同样问题的关键! !!

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

android:name="MainActivity"

android:launchMode="singleTop"

然后像哈桑·马苏德(Hasan Masud)所说的那样,您应该在操作中添加Intent.ACTION_MAIN和Intent.CATEGORY_LAUNCHER,如下所示

Intent intent = new Intent(this, MainActivity.class);

intent.setAction(Intent.ACTION_MAIN);

intent.addCategory(Intent.CATEGORY_LAUNCHER);

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

你是最棒的! 我花了很长时间才发现您需要添加Intent.ACTION_MAIN和INTENT.CATEGORY_LAUNCHER! 我还没看到它写在任何地方...

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

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐