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);
Related
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.
I am trying to send a broadcast when a pause button on a notification is clicked. The notification is fired from a background service that controls a music player.
This is the notification.
private void showPlayerNotification() {
Intent notifyIntent = new Intent(this, PlayerActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent pauseIntent = new Intent(Constants.RADIO_SERVICE_ACTION_RECEIVER);
pauseIntent.putExtra(Constants.RADIO_SERVICE_ACTION, RadioService.PAUSE_ACTION);
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(this, 1, pauseIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentIntent(pendingIntent)
.setStyle(new MediaStyle().setShowActionsInCompactView(0))
.setSmallIcon(R.drawable.play)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setColorized(true)
.setColor(getResources().getColor(R.color.player_background))
.setAutoCancel(true)
.setContentTitle(mediaStream.getStreamName())
.addAction(R.drawable.notification_pause, "Pause", pausePendingIntent);
Notification mediaNotification = notificationBuilder.build();
startForeground(1, mediaNotification);
}
The broadcast receiver is defined in the Service and is registered with an intent filter Constants.RADIO_SERVICE_ACTION_RECEIVER. I use the same broadcast receiver successfully to control playback where the broadcast originate from an activity.
The above pauseIntent broadcast is not getting through to the broadcast receiver. Could it be that the service context causing the issue?
I have successfully received the intent in my player activity by using a PendingIntent.getActivity but I don't want to handle it in the activity as that might mess the activity code.
I am using following code to open my application when clicking on Notification
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.cancel(Constants.PUSH_ID);
mNotificationManager.notify(Constants.PUSH_ID, mBuilder.build());
The problem is, if the application is already running, it opens startup application after my current activity.
Intent x = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
How can i close the old and start the application new one.
OK, i found a very cool and simple solution. I would just pass an extra boolean to my intent like i.putExtra("shouldRestart", true);
on my SplashActivity, i check
if(getIntent().getBooleanExtra("shouldRestart", false))
{
// we need to clear all activities from top
finishAffinity();
}
I have implemented some notifications in my application. It seems that everytime I click on the notification it starts a new intent. The goal is to bring the application in the foreground because it is still in background (so not killed).
How can I achieve this?
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ChatActivity.class), 0);
long[] vibration = new long[]{100, 250};
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setTicker(msg)
.setLights(Color.WHITE, 300, 600)
.setSound(Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.new_msg))
.setVibrate(vibration)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
To bring the application to the front, use the following intent in the Pending Intent.
Intent intent = new Intent(this, ChatActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
This'll resume the application if not already killed or open the ChatActivity if it has been killed.
I have successfully created a notification thanks to my other question, using NotificationCompat. I want to now just change what the notification does onClick. I'd really just like to have an alert dialog pop up (I've seen some app's do it) but everytime I click it, I just have my activity show up. Any ideas?
The notification code:
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
YOUR_PI_REQ_CODE, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setTicker(res.getString(R.string.your_ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();
nm.notify(YOUR_NOTIF_ID, n);
You can't have a normal dialog without an activity. There are several possible workarounds though including styling the activity like a dialog and making the activity itself invisible and launching a dialog from it immediately.
You can probably use Remote Views to show a dialog without going to your app process.
you can set
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigPictureStyle notiStyle = new
NotificationCompat.BigPictureStyle();
android.app.Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(en_title)
.setContentText(en_alert)
.setAutoCancel(true)
.setNumber(++numMessages)
.setStyle(notiStyle)
//.setContentIntent(resultPendingIntent)
.setContentIntent(pendingIntent)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);