【问题标题】:Do not show notification if it is already shown如果已显示通知,则不显示通知
【发布时间】:2015-09-20 04:21:48
【问题描述】:

在我的应用程序中,我希望在某些情况下显示通知。

当通知处于活动状态时,我不想再次创建通知。

我的应用中有活动识别功能,当它检测到我在车里时,它会每秒开始发出通知。

如果那里至少有一个活动通知,我该如何阻止新的构建通知?

这是我尝试过的代码:

Intent closeIntent;
        Intent showIntent;
        if (isStart){
            closeIntent = new Intent(this, SwitchButtonListener1.class);
        } else {
            closeIntent = new Intent(this, SwitchButtonListener2.class);
        }

        closeIntent.setAction("No");
        PendingIntent pendingIntentClose = PendingIntent.getBroadcast(this, 0,
                closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action closeAction = new NotificationCompat.Action(R.drawable.btn_close_gray, "No", pendingIntentClose);

        if (isStart){
            showIntent = new Intent(this, SwitchButtonListener1.class);
        } else {
            showIntent = new Intent(this, SwitchButtonListener2.class);
        }

        showIntent.setAction("Yes");
        PendingIntent pendingIntentShow = PendingIntent.getBroadcast(this, 0,
                showIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action showAction = new NotificationCompat.Action(R.drawable.ic_tick, "Yes", pendingIntentShow);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_stat_milebox)
                .setContentTitle(title)
                .setContentText(message)
                .addAction(showAction)
                .addAction(closeAction);
        builder.setSound(alarmSound);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(100, builder.build());

【问题讨论】:

  • 服务是否需要这种方式?我的意思是,如果你有一个服务并且它与这个通知类有某种联系。
  • @andrei 这个代码应该在服务里面

标签: android notifications android-notifications


【解决方案1】:

您可以尝试以下作为草图:

public class MediaNotificationManager extends BroadcastReceiver {


private final NotificationManager mNotificationManager;

private Context ctx;
private boolean mStarted = false;

public MediaNotificationManager(Context ctx) {
    mCtx = ctx;
    mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    // Cancel all notifications to handle the case where the Service was killed and
    // restarted by the system.
    mNotificationManager.cancelAll();
}

/**
 * Posts the notification and starts tracking the session to keep it
 * updated. The notification will automatically be removed if the session is
 * destroyed before {@link #stopNotification} is called.
 */
public void startNotification() {
    if (!mStarted) {

        // The notification must be updated after setting started to true
        Notification notification = createNotification();
        if (notification != null) {
            mStarted = true;
        }
    }
}

/**
 * Removes the notification and stops tracking the session. If the session
 * was destroyed this has no effect.
 */
public void stopNotification() {
    if (mStarted) {
        mStarted = false;
        try {
            mNotificationManager.cancel(NOTIFICATION_ID);
        } catch (IllegalArgumentException ex) {
            // ignore if the receiver is not registered.
        }
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    LogHelper.d(TAG, "Received intent with action " + action);
    switch (action) {
        //do something with this.            
    }
}

private Notification createNotification() {
  //create and return the notification
}

}

更多信息请阅读:
我也在我的代码中使用了这个通知:
https://github.com/googlesamples/android-UniversalMusicPlayer/blob/master/mobile/src/main/java/com/example/android/uamp/MediaNotificationManager.java

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-13
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多