7

即使用户单击它或单击全部清除,我也想让我的 Android 通知保持不变...

现在它有时会停留,有时会被删除,我不确定是什么原因造成的。

这是我的通知代码:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void createNotification()
{
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                            .setContentTitle("Wolftech Field Agent")
                            .setContentText("Getting Status")
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setOngoing(true)
                            .setAutoCancel(false);

    Intent intent = new Intent(context, FieldAgent.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(FieldAgent.class);
    stackBuilder.addNextIntent(intent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,  PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(pendingIntent);

    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

public static void updateNotificationText(String inString)
{
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                                            .setContentText(inString)
                                            .setContentTitle("Wolftech Field Agent")
                                            .setSmallIcon(R.drawable.ic_launcher)
                                            .setOngoing(true)
                                            .setAutoCancel(false);

    Intent intent = new Intent(context, FieldAgent.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(FieldAgent.class);
    stackBuilder.addNextIntent(intent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,  PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(pendingIntent);

    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

public static void cancelNotification()
{
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.cancel(NOTIFICATION_ID);
}
4

5 回答 5

10

我认为您正在寻找持续的通知,在这种情况下,如果您正在使用NotificationCompat.Builder,您可以使用:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this);
mNotifyBuilder.setOngoing(true);
于 2014-09-03T18:07:27.740 回答
4

我相信你正在寻找这个标志:

Notification.FLAG_NO_CLEAR

将该标志添加到您的通知中。

于 2013-05-31T14:23:05.760 回答
1

你试试

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,  PendingIntent.FLAG_UPDATE_CURRENT);
 notificationManager.cancel(pendingIntent);
于 2013-05-31T12:54:05.887 回答
1

好的,我意识到我发布的代码实际上很好。

发生的事情是,当我单击通知时,它再次调用 onCreate(),然后在随机间隔后调用 onDestroy()。

在 onDestroy() 我有我的 cancelNotification() 方法,所以如果 onDestroy() 在 onCreate() 之后被调用,它会删除我的通知。

这给我带来了一个新问题:为什么在我遵循了我可以在这里找到的所有关于如何阻止这种情况发生的答案之后,它会破坏和重新创建我的活动?

您可以在此处找到该问题如何使通知恢复而不是重新创建活动?如果你想帮我解决它...

于 2013-06-03T13:23:10.807 回答
-1

我也有这个问题。我的解决方案是在创建意图的函数上添加额外内容

        private void addNotification(String headLine, String info, Context context) {
        mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.icon)  //R.drawable.icon
                        .setContentTitle(headLine)
                        .setContentText(info)
                        .setOngoing(true)
                        .setAutoCancel(false);
// Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, MainActivity.class);
        resultIntent.putExtra("FROM_NOTIF", true);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
        //      stackBuilder.addParentStack(MainActivity.class);
        //Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
        //      mBuilder.setf |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        mNotificationManager.notify(mNotification_id, mBuilder.build());
    }

在 MainActivity 中:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.getBooleanExtra("FROM_NOTIF", false)) {
        addNotification(...)
            ... call the function that adds the notification again ..}

所以事情是通知在用户按下它后关闭,但是你可以在开始使用 onNewIntent() 的活动上收到通知,检查那里的意图,如果你发现它来自通知,设置它再次。

于 2017-06-18T12:22:23.157 回答