通知仅显示一次,此后不再显示

时间:2019-01-25 13:53:05

标签: java android notifications

我正在安排一个FirebaseJob,一旦完成,它就会在下一个窗口ON_ANY_NETWORK上再次进行调度。它的工作做得很好,符合预期。我从中调用服务,每次使用待定的意图都会创建一个新的通知。像:

Intent resultIntent = new Intent(this, MainActivity.class);
    resultIntent.putExtra("NotificationMessage", "NotificationMessage");
    //resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("My App name")
            .setContentText("Content text")
            .setTicker("This is a ticker text")
            .setStyle(new NotificationCompat.BigTextStyle().bigText("This is a long text"))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(resultPendingIntent)
            .setAutoCancel(false)
            .setOngoing(true)
            .setColor(getResources().getColor(android.R.color.holo_red_dark));

    if (notificationManager != null)
    {
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
正如我所期望的那样,

这是第一次显示该通知。我不希望任何用户刷卡,直到再次打开该应用程序。从该通知中打开应用后,我将取消通知:

int MY_NOTIFICATION_ID= 234;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.cancel(MY_NOTIFICATION_ID);

但是当我的工作再次开始执行并执行上面显示的用于创建通知的相同代码时,它将执行该代码,但是通知不会再次显示。董事会成员将提供任何帮助。预先感谢。

1 个答案:

答案 0 :(得分:0)

这是因为您的MY_NOTIFICATION_ID始终相同。相反,例如,您可以将MY_NOTIFICATION_ID设置为等于System.currentTimeMillis()

int MY_NOTIFICATION_ID= System.currentTimeMillis();
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.cancel(MY_NOTIFICATION_ID);
相关问题