当活动要求完成通知pendingIntent contentIntent失败() [英] Notification pendingIntent contentIntent fails when activity calls finish()
问题描述
我有一个前台服务通知,当点击应该开始一个活动。它调用完成()之前,此活动是非常短暂的。
I have a foreground service notification that when clicked should start an activity. This activity is very short lived before it calls finish().
第一次通知,点击它的工作原理,第二次,然后我得到的错误:
The first time the notification is clicked it works, the second time and thereafter I get the error:
Sending contentIntent failed: android.app.PendingIntent$CanceledException
在我的code创建前台服务通知的时候,我已经改变了randomActivity.class到不调用完成另一个活动类和它完美的作品在每一次点击。发件人:
In my code when creating the foreground service notification, I've changed the randomActivity.class to another Activity class that does not call finish and it works perfectly on every click. From:
Intent notificationIntent = new Intent(this, RandomActivity.class);
到
Intent notificationIntent = new Intent(this, HomeActivity.class);
做工精细...
Works fine...
我已经使用了标准通知code从Android开发者网站,以及使用通知建设者测试。我得到了相同的结果而不管。它的工作原理完全除非活动调用完成();
I've used the standard notification code from the Android Developers website, as well as testing it using Notification builder. I get the same result regardless. It works perfectly unless the Activity calls finish();
这是预期的行为,错误,还是我失去了一些东西?
您的帮助,我感谢你在有希望的解决方案!
I thank you in advance for you help and hopefully a solution!
注:通知code我用的是完全标准的,所以我没有把它贴。 RandomActivity称光洁度();在的onCreate,所以没有什么不寻常的看到有两种。
Note: The notification code I use is completely standard, so I haven't posted it. RandomActivity calls finish(); in onCreate, so there's nothing unusual to see there either.
推荐答案
乱投医后,我可能会,我终于找到了解决办法。发帖万一有人过这个问题绊倒了。
After trying everything I possibly could, I eventually found a solution. Posting in case anyone stumbles across this issue too.
我不得不匹配INT请求code的通知ID。为什么?完全不知道......我只能假设这$成为空或重新使用p $ pvents的意图数据?
I had to match the int requestCode to the notification id. Why? Absolutely no idea... I can only assume it prevents the intent data from becoming null or reusing it?
private static int ONGOING_NOTIFICATION_ID = 76;
PendingIntent contentIntent = PendingIntent.getActivity(this,
ONGOING_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
同为通知ID为startForeground:
The same as the notification id to startForeground:
this.startForeground(ONGOING_NOTIFICATION_ID, not);
希望这可以帮助别人。
Hope this helps someone.
这篇关于当活动要求完成通知pendingIntent contentIntent失败()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!