我看到了几种方法,我尝试了所有的方法,但都不能工作,我不知道为什么它如此复杂,在文档中它看起来如此简单!我希望通过通知触发OnNewIntent (用户在通知栏中单击它)。
目前,我已将我的活动设置为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调用。我不知道我做错了什么。我在整个应用程序中只使用了两个activites,并且MainMenuActivity在LoginActivity之后,所以MainMenuActivity无论如何都应该在堆栈的顶部(我在MainMenuActivity中有更多的片段来替换它们)。
任何帮助都将不胜感激!谢谢你们。
发布于 2015-07-16 11:42:03
Ok在发布我的问题后很快就让它正常工作了。我认为我们代码中的关键区别在于,我将"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());发布于 2017-08-26 00:30:35
首先,您应该将android:launchMode="singleTop"添加到清单文件中的活动定义中,如下所示
<activity
android:name="MainActivity"
android:launchMode="singleTop"
</activity>然后就像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(..)当前活动的方法。
发布于 2017-07-10 22:10:52
经过长时间的检查,我注意到Android4.4 (Kitkat或更低版本)不会调用onNewIntent和onResume,如果你在pendingIntent上使用FLAG_UPDATE_CURRENT的话。一旦您将其更改为FLAG_ONE_SHOT,它将开始工作。然而,在Android6(可能还有5)上,即使使用FLAG_UPDATE_CURRENT标志,onNewIntent也能正常工作。
但是,如果您创建了多个挂起的意图(例如,在收到两个通知之后),数据将不会使用标志FLAG_ONE_SHOT进行更新,因此标志FLAG_CANCEL_CURRENT可能是更好的选择(未更新的数据没有问题)
https://stackoverflow.com/questions/30708462
复制相似问题