You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

应用关闭时Notification显示null问题排查求助

Hey there! Let's break down why your notification shows null when the app is closed, and how to fix it based on your code snippet:

Core Issue: Data Persistence Across App Process Death

When your app is closed (and its process is killed), any static variables or in-memory data you're using to store the notification text gets reset to null. Your code passes nazwaLeku.getText().toString() to NotificationReceiver.xd(), which I suspect is storing this value in a static field—this won't survive when the app isn't running.

Step-by-Step Fixes & Checks

1. Audit NotificationReceiver.xd() Implementation

First, look at how you're storing the notification text in the receiver. If it's using a static variable like this, that's the problem:

public class NotificationReceiver extends BroadcastReceiver {
    // This static field gets cleared when the app process dies
    public static String sNotificationText;

    public static void xd(String text) {
        sNotificationText = text;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // sNotificationText will be null if the app was closed
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentText(sNotificationText);
    }
}

2. Pass Data via Intent Extras (The Correct Approach)

Instead of relying on static variables, pass the notification text directly through the Intent that triggers the receiver. This ensures the data is preserved even when the app isn't running.

Update Your Click Listener Code

dodaj.setOnClickListener(new View.OnClickListener() {
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View view) {
        String medicationText = nazwaLeku.getText().toString();
        
        // Create an Intent targeting your NotificationReceiver
        Intent notificationIntent = new Intent(getApplicationContext(), NotificationReceiver.class);
        // Attach the text as an extra
        notificationIntent.putExtra("MEDICATION_REMINDER_TEXT", medicationText);
        
        // Create a PendingIntent (use FLAG_IMMUTABLE for Android 12+, add FLAG_UPDATE_CURRENT to refresh extras)
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(),
                0,
                notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );
        
        // Schedule the notification (using AlarmManager for timed reminders, for example)
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        long triggerTime = /* Your desired trigger time in milliseconds */;
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
    }
});

Update NotificationReceiver to Retrieve the Extra

@Override
public void onReceive(Context context, Intent intent) {
    // Get the text from the Intent extra (fallback to a default if it's null)
    String reminderText = intent.getStringExtra("MEDICATION_REMINDER_TEXT");
    if (reminderText == null) {
        reminderText = "Medication reminder";
    }

    // Build and show the notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "YOUR_CHANNEL_ID")
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle("Take your medication")
            .setContentText(reminderText)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(1, builder.build());
}

3. Verify Notification Channel Setup (Android O+)

While this isn't the direct cause of the null issue, ensure you've created a valid notification channel (required for Android 8.0+):

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Medication Reminders";
        String description = "Channel for medication reminder notifications";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID", name, importance);
        channel.setDescription(description);
        
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
Key Takeaway

Static variables don't persist when the app's process is killed. By passing your notification text through Intent extras, you ensure the data is available to the receiver even when the app isn't running.

内容的提问来源于stack exchange,提问作者laquan.motaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:15:57