Android notification click to open app only works 1st time - java

I have a foreground service with a notification. I create the notification like this:
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setOngoing(true)
.setContentTitle("App name")
.setContentText(Utility.getNotificationContentText())
.setSmallIcon(R.mipmap.ic_launcher_round)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setOnlyAlertOnce(true)
.build();
On the notification I set a pending intent like so:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
When I click the notification the first time, it works as expected. It opens the app. However, when I click the notification the second time, nothing happens.
For what it's worth, I open the MainActivity.class, but my app has different fragments. I would like to open a specific fragment, but I am not sure if I can pass FragmentName.class into the intent.
Any help for getting the notification click to work every time?

Set the below flags of the PendingIntet so that you can update if there is a current pending intent with the new PendingIntent.
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

I managed to fix it. The problem was that I was updating the notification with a intent that had the wrong class.

Related

Activity won't launch from notification when the app is started from notification

I have an app that sends notifications, and when clicked, opens an activity from my app. If you launch the app and keep the app open and click notifications, it works properly. However, when the app is closed, it doesn't. This is what happens:
Close app > receive notification > click notification > opens the correct activity > receive another notification > nothing happens when clicked
The intended functionality is for the notification to open the activity regardless if it is already open, and just add it onto the task stack. So if you clicked 3 notifications, the stack would be ActivityA > ActivityA > ActivityA. Here is the code for the notification:
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(ID, new NotificationCompat.Builder(context, CHANNEL_ID)
.setOnlyAlertOnce(false)
.setAutoCancel(true)
.setCustomContentView(helper.createSmallView(context))
.setCustomBigContentView(helper.createBigView(context))
.setContentIntent(PendingIntent.getActivity(context, 1,
new Intent(context, ActivityA.class),
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE)
)
.build());
I also tried using a broadcast PendingIntent. The BroadcastReceiver would be called but the call to Context#startActivity did nothing.
Intent intent = new Intent(YourActivity.class, NewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
PendingIntent
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(ID, new NotificationCompat.Builder(context, CHANNEL_ID)
.setOnlyAlertOnce(false)
.setAutoCancel(true)
.setCustomContentView(helper.createSmallView(context))
.setCustomBigContentView(helper.createBigView(context))
.setContentIntent(pendingIntent)
.build());

Create Android java notification for Flutter package and redirect to the app when clicked

I'm creating a flutter package. I am creating a notification in the Android java section, but when I click on this notification, I want to go to the application, but I can't figure out how to do it.
My notification generation code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL)
.setSmallIcon(R.drawable.ic_notification)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(publicVersion ? NotificationCompat.VISIBILITY_PUBLIC
: NotificationCompat.VISIBILITY_PRIVATE);
builder.setContentTitle(getString(s));
return builder.build();
You can use pending intent : https://developer.android.com/training/notify-user/navigation#java
// Create an Intent for the activity you want to start
Intent resultIntent = new Intent(this, ResultActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(resultPendingIntent);

How to start Flutter application from foreground service

I have a Flutter app, which runs a never ending foreground service and it is defined like this:
private void startForeground() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
startForeground(NOTIF_ID, new NotificationCompat.Builder(this,
NOTIF_CHANNEL_ID)
.setOngoing(true)
.setContentTitle("service")
.setContentText("Service is running background")
.setContentIntent(pendingIntent)
.build());
}
and I start service like this:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.smile)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
My foreground service is showing notification all the time.
Is it possible to start a Flutter app when user press on notification which is showed by foreground service, even if the Flutter app isn't running in background? Thanks.
To launch an Activity when clicking on a notification, you can use the PendingIntent. You already have this implemented in the first sample, but here are some links. Android docs Stackoverflow question
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
builder.setContentIntent(pendingIntent);

Putting an View object in notification android

I am trying to put a View object (such as TextView, Edittext, Button, etc) in a notification in Android. I am new to this assignment and I am confused so here is my attempt so far.
So I have a notification to appear as following:
Intent intent = new Intent(context, Receiver.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("Message From")
.setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "", pIntent).build();
NotificationManager notificationManager =
(NotificationManager)
context.getSystemService(context.NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
I know this can be done for Jelly Bean and after version. I was wondering if its possible to insert a View object possibly through PendingIntent using Intent?
If anyone can guide me in the right direction that would be great
see these
http://www.framentos.com/en/android-tutorial/2012/02/20/how-to-create-a-custom-notification-on-android/
Create custom notification, android

Notification opening Google play

I would like make a notification when clicking open Google Play.
I'm using a google Api example.
Another question is, the notification Id can be static one?
Is neceesary add some other stuff in androidmanifest?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
Notification ID is only for Notification identification and also if the previous notification is still visible, the system updates it from the contents of the Notification object. If the previous notification has been dismissed, a new notification is created instead.
You must add your intent ResultActivity into your manifest.xml file and also if you set Notification sound then you must add Vibrate permission into your manifest.xml file that's it.

Categories

Resources