Android 通知getIntent().getExtras()获取相同的捆绑包
我遇到的问题是我调用sendAffimationNotice()两次,每次都传递一个唯一的Android 通知getIntent().getExtras()获取相同的捆绑包,android,Android,我遇到的问题是我调用sendAffimationNotice()两次,每次都传递一个唯一的aff_id(通过打印“put”+aff_id确认) 这将打印“放置1”,并在第二次调用“放置2” 手机现在有2个通知。尽管当两个都被单击时,它们在intents活动的onCreate方法中都有相同的ID 两次打印“ID为:1”,即使两个通知都是唯一的 public class PossAffNotification{ private static int notif_ID = 1; private st
aff_id
(通过打印“put”+aff_id
确认)
这将打印“放置1”
,并在第二次调用“放置2”
手机现在有2个通知。尽管当两个都被单击时,它们在intents活动的onCreate方法中都有相同的ID
两次打印“ID为:1”,即使两个通知都是唯一的
public class PossAffNotification{
private static int notif_ID = 1;
private static NotificationManager notificationManager;
private static Notification note;
private static PendingIntent contentIntent;
public static void sendAffimationNotice(Context ctx, String title,String contentText,int aff_id){
notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
note = new Notification(android.R.drawable.btn_star_big_on, contentText, System.currentTimeMillis() );
note.defaults |= Notification.DEFAULT_SOUND;
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.FLAG_AUTO_CANCEL;
note.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(ctx, com.mindfsck.PossAff.MainActivity.class);
notificationIntent.putExtra("aff_id",aff_id);
System.out.println("putting " + aff_id);
notificationIntent.setAction("com.mindfsck.PossAff.intent.action.aff");
notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
note.setLatestEventInfo(ctx, title, contentText, contentIntent);
notificationManager.notify(notif_ID,note);
notif_ID += 1;
}
};
public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent serviceIntent = new Intent(this,PAService.class);
this.startService(serviceIntent);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
Toast.makeText(getApplicationContext(), "ID is: " + extras.getInt("aff_id"),
Toast.LENGTH_SHORT).show();
}
}
}
您的
意图
在所有非额外参数(如操作)上都是相同的。因此,您可以从getActivity()
调用中获得相同的pendingent
,因为每个不同的Intent
只有一个pendingent
。您需要在第二个意图中更改一些内容,而不是附加内容,例如不同的动作字符串。我的这篇文章可能会对您有所帮助:
这个问题基本上来自这样一个事实,即系统并不认为仅仅因为我们将一个新的意图对象传递给挂起的意图,我们就希望这个新的意图对象被传递,而只是让旧的(初始的)挂起的意图保持活动
为了获得所需的语义,我们需要传递一个标志来告诉系统:
PendingIntent pintent =
PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
此问题的问题来自以下行:
contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
最后一个参数不应为0,它应为pendingent.FLAG\u CANCEL\u CURRENT
contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
否则,在notificationIntent中,您将始终收到相同的数据谢谢,这将更详细地解释Commonware Link描述的问题已经腐烂。这在理论上可以回答这个问题,但最好在这里为未来的用户提供答案的基本部分,并提供链接供参考。可以通过以下方式变得无效。