'Android Studio app restarts upon notification click, no matter what I try
I am honestly starting to believe the issue reported in the title is an issue with the new Android API required to publish on the store. I have tried setting android:launchMode="singleTask" in the manifest, and below is my code to build and show the notification. I have tweaked and tweaked but whenever the notification is pressed, the app restarts.
//other info: Android Studio: 2021.3 Emulated: Pixel 4 XL API 30 SDK: Android 11 Also tested on physical device: true Cache invalidated: true
//CODE
private void showNotification(int notificationID, String title, String message, String channel_id, String channelName, String channelDescription, int notifIcon)
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(channel_id, channelName, importance);
mChannel.setDescription(channelDescription);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.enableLights(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
mChannel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channel_id).setSmallIcon(notifIcon).setContentTitle(title).setContentText(message);
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
builder.setOngoing(true);//s that it can't just be swiped away!
//
notificationManager.notify(notificationID, builder.build());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
