简体   繁体   English

android挂起意图通知问题

[英]android pending intent notification problem

I have a alarm thing going on in my app and it launches a notification that then when pressed launched an activity.我的应用程序中有一个警报事件,它会启动一个通知,然后在按下时启动一个活动。 The problem is that when I create more than one alarm then the activity launched from the notification gets the same extras as the first one.问题是,当我创建多个警报时,从通知启动的活动将获得与第一个相同的附加功能。 I think the problem is either with the intent i put in the pending intent or in the pending intent itself.我认为问题要么出在我放入待处理意图中的意图,要么出在待处理意图本身中。 I think I might need to put a flag on one of these but I dont know which one.我想我可能需要在其中一个上放一面旗帜,但我不知道是哪一个。

Intent showIntent =new Intent(context, notificationreceiver.class);
    showIntent.putExtra("details", alarmname);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
        showIntent, 0); 

    notification.setLatestEventInfo(context, "The event is imminent",
            alarmname, contentIntent);

And the receiver of the notification和通知的接收者

Bundle b = getIntent().getExtras();
    String eventname = b.getString("details");
    details.setText(eventname);

The "details" extra is the same to every the next time a notification happens instead of having the different value.每次发生通知时,“详细信息”额外内容都相同,而不是具有不同的值。 Until I set the intents I am sure that the correct value goes to the "details" so its a problem of getting the first intent everytime i press any notification.在我设置意图之前,我确信正确的值会出现在“详细信息”中,因此每次按下任何通知时都会出现第一个意图的问题。 How can I make it to launch the correct intents?我怎样才能让它启动正确的意图? Hope I was as clear as i could Thanks!希望我尽可能清楚谢谢!

The way I solved that problem was by assigning a unique requestCode when you get the PendingIntent:我解决这个问题的方法是在获得 PendingIntent 时分配一个唯一的requestCode

PendingIntent.getActivity(context, requestCode, showIntent, 0); 

By doing so you are registering with the system different/unique intent instances.通过这样做,您正在向系统注册不同/独特的意图实例。 Tip: A good way of making the requestCode unique would be by passing to it the current system time.提示:使 requestCode 唯一的一个好方法是将当前系统时间传递给它。

int requestID = (int) System.currentTimeMillis();

The problem is that when I create more than one alarm then the activity launched from the notification gets the same extras as the first one.问题是,当我创建多个警报时,从通知启动的活动将获得与第一个相同的附加功能。

Correct.正确的。

How can I make it to launch the correct intents?我怎样才能让它启动正确的意图?

That depends on whether you have two alarms that will be registered at once, or not.这取决于您是否有两个将同时注册的警报。

If not, you can use FLAG_ONE_SHOT or one of the other PendingIntent flags to have your second PendingIntent use the newer extras.如果没有,您可以使用FLAG_ONE_SHOT或其他PendingIntent标志之一让您的第二个PendingIntent使用较新的附加功能。

If, however, you will have two alarms registered at once, with different Intent extras, you will need to make the two Intents be more materially different, such that filterEquals() returns false when comparing the two.但是,如果您将同时注册两个警报,并且具有不同的Intent附加项,则需要使这两个Intents具有更大的实质性不同,以便在比较两者时filterEquals()返回false For example, you could call setData() or setAction() and provide different values for each Intent .例如,您可以调用setData()setAction()并为每个Intent提供不同的值。

I had this issue in my app and just generated a random number to over come overriding notifications intent:我在我的应用程序中遇到了这个问题,只是生成了一个随机数来覆盖通知意图:

int random= new Random().nextInt();
PendingIntent resultPendingIntent =
      stackBuilder.getPendingIntent(
              random,
              PendingIntent.FLAG_UPDATE_CURRENT
      );

I followed the solution provided by U-ramos and this worked for me我遵循了 U-ramos 提供的解决方案,这对我有用

int requestID = (int) System.currentTimeMillis();

PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, showIntent, 0);

another solution:另一种解决方案:

use the PendingIntent.FLAG_UPDATE_CURRENT like this:像这样使用PendingIntent.FLAG_UPDATE_CURRENT

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,i, PendingIntent.FLAG_UPDATE_CURRENT);

this worked for me这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM