在Android中,我如何知道当前的通知id来清除通知



现在在android中,我把这段代码放在一个活动中,当按钮被按下时显示通知。

static int notificationCount = 0;
然后

 btnNotification.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent notificationIntent = new Intent(AlertsActivity.this,NotificationActivitty.class);
                    PendingIntent pIntent = PendingIntent.getActivity(AlertsActivity.this,notificationCount,notificationIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
                    // Construct the notification
                    Notification.Builder nBuilder = new Notification.Builder(AlertsActivity.this);
                    nBuilder.setContentTitle("You Have a notification!");
                    nBuilder.setContentText("See Your Notification");
                    nBuilder.setSmallIcon(android.R.drawable.btn_star);
                    nBuilder.setContentIntent(pIntent);
                   nBuilder.addAction(android.R.drawable.stat_notify_call_mute, "go to", pIntent); // from icecream sandwatch - required api 16
                    // Build the notification
                    Notification noti = nBuilder.build(); // required api 16
                    //Send it to manager
                        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                        manager.notify(notificationCount++,noti);
                }
            }
    );

从通知管理器,任何通知,我点击它,它会重定向到另一个活动(NotificationActivity)

现在我用这段代码来清除通知,但它只清除id为0的通知,所以我怎么才能清除当前按下的通知

public class NotificationActivitty  extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);
    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(0);
    // manager.cancelAll(); // Cancel all notifications for this app. from manager
}

如果可能的话,我需要通过它的id清除通知

您应该为通知添加一个Tag,然后通过提供正确的id和正确的Tag来清除通知。

如果你传递相同的id,你不需要通知计数器,因为当通知看到相同的id时,它会清除旧的通知并放置一个新的,除非你想显示用户收到了多个通知。

private static final String TAG = "YourNotification";
private static final int NOTIFICATION_ID = 101;
private Notification notification;
public NotificationManager notificationManager;
//you can create notification with it's own id and tag, text, etc by passing 
//these variables into the method (int id, String tag, ... etc)
public void createNotification()
{
    notification = new Notification.Builder(context)
                       .setContentTitle("Content title")
                       .setContentText("Content text")
                       .setSmallIcon(R.drawable.your_small_icon)
                       .setLargeIcon(bitmapYourLargeIcon)
                       .setContentIntent(pendingIntent)
                       .addAction(R.drawable.icon, pendingIntentAction)
                       .build();
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(TAG, NOTIFICATION_ID, notification);
}

取消通知只需使用这个方法:

//clears your notification in 100% cases
public void cancelNotification(int id, String tag)
{
  //you can get notificationManager like this:
  //notificationManage r= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(tag, id);
}

您需要在创建通知时添加这行代码!!

notification.flags |= Notification.FLAG_AUTO_CANCEL;

点击时取消通知。

进一步阅读:点击通知后打开应用程序

**编辑,添加一个额外的,以确定是否某些通知被点击pIntent.putExtra("fromNotification", true);

也可以使用通知通道https://developer.android.com/develop/ui/views/notifications#ManageChannels

相关内容

  • 没有找到相关文章

最新更新