Android 5.1.1按钮触发通知实现技术求助
Hey Ewan, let's get that notification working smoothly on your Android 5.1.1 device! The key issue here is that Android 8.0 (API 26) introduced Notification Channels, but your target version doesn't require them—so we need to adjust the code to skip channel setup for older OS versions while keeping compatibility for future use.
Here's the step-by-step fix for your Activity:
Use
NotificationCompat.Builderfor cross-version compatibility
This class from the AndroidX (or Support) library handles the differences between old and new Android versions automatically, so you don't have to write separate code paths.Add version checks for Notification Channels
Since Notification Channels don't exist before API 26, wrapping that code in a version check prevents crashes on Android 5.1.1.Ensure mandatory notification fields are set
Android requires a small icon for all notifications—don't skip this, or your notification won't show up.
Modified Code Example:
import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import androidx.core.app.NotificationCompat; // Inside your button click listener or notification method public void sendNotification() { // Get the NotificationManager system service NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // Unique ID for your notification (use any integer) int notificationId = 1; String channelId = "school_app_channel"; // For future API 26+ compatibility // Build the notification content NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notification) // Replace with your app's small icon .setContentTitle("Task Reminder") .setContentText("Your school task notification is here!") .setPriority(NotificationCompat.PRIORITY_DEFAULT); // Sets priority for pre-API 26 // Create a PendingIntent to open your Activity when the notification is clicked Intent intent = new Intent(this, YourMainActivity.class); // Replace with your Activity class PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT // Ensures the intent updates if needed ); notificationBuilder.setContentIntent(pendingIntent); // Only create a Notification Channel if running on API 26 or higher if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence channelName = "School App Notifications"; String channelDescription = "Notifications for your school task app"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); channel.setDescription(channelDescription); // Register the channel with the system notificationManager.createNotificationChannel(channel); } // Finally, send the notification notificationManager.notify(notificationId, notificationBuilder.build()); }
Key Notes:
- Dependency Check: Make sure your app includes the AndroidX Core library in your
build.gradle(Module level):
If you're using the old Support Library instead, use:implementation 'androidx.core:core:1.10.1'implementation 'com.android.support:support-v4:28.0.0' - Small Icon: Replace
R.drawable.ic_notificationwith an actual drawable resource in your project—this is non-negotiable for notifications to appear. - PendingIntent: Adjust the
Intenttarget to match the Activity you want to open when the user taps the notification.
Just call this sendNotification() method from your button's click listener, and your Android 5.1.1 device should display the notification properly!
内容的提问来源于stack exchange,提问作者Ewan Arends

