The Code A is a sample code about Notifications in the project user-interface-samples.
I trun off the notifications of the app, then run the app, an information "You need to enable notifications for this app" is displayed.
When I click the Snackbar on API 28 emulator, the following error is displayed:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.settings/com.android.settings.Settings$AppNotificationSettingsActivity}: java.lang.NullPointerException: Attempt to read from field 'android.content.pm.ApplicationInfo android.content.pm.PackageInfo.applicationInfo' on a null object reference
When I click the Snackbar on API 30 emulator , the prompt message is displayed, see Image A.
The app wasn't found in the list of installed app.
What are problems with the sample code?
Code A
mNotificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
boolean areNotificationsEnabled = mNotificationManagerCompat.areNotificationsEnabled();
if (!areNotificationsEnabled) {
// Because the user took an action to create a notification, we create a prompt to let
// the user re-enable notifications for this application again.
Snackbar snackbar = Snackbar
.make(
mMainRelativeLayout,
"You need to enable notifications for this app",
Snackbar.LENGTH_LONG)
.setAction("ENABLE", new View.OnClickListener() {
@Override
public void onClick(View view) {
// Links to this app's notification settings
openNotificationSettingsForApp();
}
});
snackbar.show();
return;
}
private void openNotificationSettingsForApp() {
// Links to this app's notification settings.
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
startActivity(intent);
}
Image A
