【问题标题】:Notification auto cancel not working for Android lollipop通知自动取消不适用于 Android 棒棒糖
【发布时间】:2015-01-09 20:31:19
【问题描述】:

我想在用户点击通知时自动取消我的通知。以下代码适用于除 Android lollipop 设备之外的所有设备。在 Lollipop 设备中,只有当用户滑动它时才会发出通知。

@SuppressWarnings("deprecation")
public void sendNotification(int id){

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Test")
            .setContentText("Jump to next screen")
            .setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(true);

    mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;


    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, NextActivity.class);

   resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
                                    resultIntent, 0);
    //PendingIntent.FLAG_UPDATE_CURRENT

    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // id, if there is a need to update the notification later on.
    mNotificationManager.notify(id, mBuilder.build());

    Log.v(TAG, "Notification ID value " + id);

    //mNotificationManager.cancel(id);
}

这段代码缺少什么?

【问题讨论】:

  • 即使我也看到了这个问题。你能找到工作吗?我看到的是,它被删除了,并且通知中心发生了任何刷新。
  • 以下。我也卡住了。到处查看,但还没有解决这个问题。我目前正在运行一个计时器并在计时器用完后取消所有通知。

标签: android notifications push-notification android-notifications notificationmanager


【解决方案1】:

我觉得不错。我的自动取消仍然适用于 5.0.2。让我给你我的一部分代码:

public static void notif(int id, String title, String text, Context context, Class activity) {
    // get an instance of the NotificationManager service
    if (mNotifyMgr == null)
        mNotifyMgr = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(context, activity);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.launcher)
            .setContentTitle(title)
            .setContentText(text)
            .setContentIntent(notifIntent);
    mNotifyMgr.notify(id, mBuilder.build());
}

【讨论】:

    【解决方案2】:

    修改如下: 设置自动取消(真)-> 设置自动取消(假); 然后在动作活动中执行以下操作

    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancelAll();
    

    如果你想设置特殊条件,你可以设置

    intent.putExtra("key", "value")
    

    【讨论】:

      猜你喜欢
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多