我有一个前台服务,我想让通知在用户单击通知时打开 MainActivity。
自定义Service类中创建Notification的方法是:
public void setForeground(final int notificationID, final int notificationIconID,
final String notificationTickerText,
final String notificationTitle, final String notificationText){
Notification notification = new Notification(notificationIconID, notificationTickerText,
System.currentTimeMillis());
notification.setLatestEventInfo(this, notificationTitle,
notificationText, PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()), 0));
startForeground(notificationID, notification);
}
如何修改它以包含打开(如果 MainActivity 当前打开)或在通知单击功能上启动 MainActivity?
最佳答案
如果你想通过点击通知
来打开任何Activity
,那么你需要实现TaskStackBuilder
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
关于java - Android 服务通知 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28925461/