通知Android不关闭后点击 [英] notification android does not close after click
问题描述
我试图创建通知果冻豆(API 16),并有得到问题,我的通知,这是我的code
i'm trying create notification jelly bean (api 16) and have get issue with my notification, this my code
public class CreateNotification extends AsyncTask<Void, Void, Void> {
int style = NORMAL;
public CreateNotification(int style) {
this.style = style;
}
@Override
protected Void doInBackground(Void... params) {
Notification noti = new Notification();
noti = setNormalNotification();
noti.defaults |= Notification.DEFAULT_LIGHTS;
noti.defaults |= Notification.DEFAULT_VIBRATE;
noti.defaults |= Notification.DEFAULT_SOUND;
noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
mNotificationManager.notify(0, noti);
return null;
}
}
private Notification setNormalNotification() {
Bitmap remote_picture = null;
remote_picture = getBitmapFromURL(sample_url);
// Setup an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, DetailActivity.class);
// TaskStackBuilder ensures that the back button follows the recommended convention for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself).
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);
return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setLargeIcon(remote_picture)
.setContentIntent(resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Open detail", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Close", resultPendingIntent)
.setContentTitle("Normal Notification")
.setContentText("This is an example of a Normal Style.").build();
}
在此行
.addAction(R.drawable.ic_launcher, "Open detail", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Close", resultPendingIntent)
如果我点击开放详细或关闭按钮,通知并不密切。如何解决?谢谢你,我对不起engglish
if i click open detail or close button, notification doesn't close.. how to fix it ? thank you, sorry with my engglish
推荐答案
当你创建你的 resultIntent 的补充通知ID。在你的情况下,它是 0 的你定义mNotificationManager.notify(0,NotI位);
所以加这样的:
When you create your resultIntent add the notification id. In your case it is the 0 you defined in mNotificationManager.notify(0, noti);
so add it like this:
resultIntent.putExtra("NOTIFICATION_ID", 0);
然后你可以检索ID取消您resultPendingIntent像这样的活动的OnCreate通知:
then you can retrieve that id to cancel the notification on the onCreate of the the activity of your resultPendingIntent like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(getIntent().getIntExtra("NOTIFICATION_ID", -1));
//todo
}
这篇关于通知Android不关闭后点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!