【问题标题】:In Android how can i know the current notification id to clear the notification在 Android 中,我如何知道当前的通知 ID 以清除通知
【发布时间】:2015-07-24 16:15:04
【问题描述】:

现在在 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 清除通知。

【问题讨论】:

    标签: android android-intent notifications android-notifications notificationmanager


    【解决方案1】:

    您应该在通知中添加标签,然后通过提供正确的 id 和正确的标签来清除通知。

    如果您传递相同的 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);
    }
    

    【讨论】:

      【解决方案2】:

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

      notification.flags |= Notification.FLAG_AUTO_CANCEL;
      

      这将取消点击通知。

      延伸阅读:Open application after clicking on Notification

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

      【讨论】:

      • 谢谢,这确实是一个很棒的方法,但是如果我想控制来自其他活动的通知,那么我怎样才能获得点击通知的通知 id,这可能吗??
      • pIntent.putExtra("fromNotification", true);然后检查活动中的额外内容。这有帮助吗? :)
      • 发现这是完成我需要的最简单的方法。谢谢大家!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      相关资源
      最近更新 更多