Open Activity from Notification click when app is killed? - android

I am implementing calling functionality in my application with Twilio SDK. I am showing notification as soon as call starts, so the user can hang up the call from the notification bar. The problem is if user kills my application forcefully, I am not able to open the same calling activity from notification tray as my application is killed.
1: How can I detect if my app gets killed.
2: How Google play music notification works ( on click of notification
it open the song detail activity - even though the application is
killed).
3: How can I retain the same objects initialized at the time of
creating the activity to disconnect the call.

Try this
Intent intent = new Intent(context, NotificationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("title")
.setContentText("Test notification")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH);
hope this help

Related

clicking on group of notification is not redirecting to specific screen, instead of that app is restarted in oreo

I have implemented firebasse notification functionality in my chat app and everything is running fine but now while testing in Android os 8.0 and 8.1 when app is in background and if user is getting 4 or more than 4 notification then its combined in group and when user click on group then not getting intent and app is restarted.
If user tap on single notification then I am able to send him in specific screen.
I want Notification data or chat id so I can send him in specific screen but not getting any data in intent.
I have searched similar kind of question in stackoverflow but still not getting proper result.
Android: Clicking Grouped Notifications Restarts App
How to open non-launcher activity on notification group click
You can use setContentIntent to set the value of Intent like below
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.your_notification_icon)
.setContentTitle("Notification Title")
.setContentText("Notification ")
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
It will open SecondActivity (Replace the SecondActivity with your own activity)
I am having the same issue causing app restart in Oreo, after many tries i noticed that if when creating the notification i call .setGroup(string) the system no longer groups the notifications.I am creating the notifications from a service. This is not a solution i know but not grouping is a lesser evil than app restart.

How to start close application and restart after click on Notificaton?

In my application i get notification and if the user click on the notification the app should start.
if the app already opened and the user click on the notification. the app open again but twice.
Now, the idea is that i need to call refreshActionBarSpinner() when the app is started.
the refreshActionBarSpinner() method is in MainActivity.class -> onStart().
if i'm using PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); the app is started but
onStart() doesn't called.
if i'm using PendingIntent.getActivity(mContext, 0, new Intent(mContext , MainActivity.class), 0); the app opened twice
this the full code:
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent openAppOnClick = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.workcloc_icon)
.setContentTitle(title)
.setContentText(content)
.setContentIntent(openAppOnClick)
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(alarmSound)
.setAutoCancel(true)
.build();
notificationManager.notify(UNIQ_ID_NOTIFICATION, notification);
So, what I need is: if the app already opend in background then call the refreshActionBarSpinner() and if it doesn't just open the app.
How can i do it please?
I noticed that if i using android:launchMode="singleTop" its works great!
it's recommended?
If an existing activity responds to a new intent, then onNewIntent() will be called. If you want refreshActionBarSpinner() to be called in that instance, you should add a call to it in the onNewIntent() callback.

notification intent opens the apps main activity in background if its paused

I have a notification service running that will create a notification for the user and if the user clicks the notification it opens an activity to display the message, the method I am using to create the notification is :
public void createNotificationMsg(String name,String doing, boolean persistent){
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, new Intent(getApplicationContext(), MessageNotificationActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP).putExtra("message", doing),
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.my_logo_96)
.setContentTitle(name)
.setContentText(doing)
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setColor(Color.parseColor("#1aadce"));
if (persistent){
notificationBuilder.setOngoing(true);
}
else {
notificationBuilder.setOngoing(false);
}
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getApplicationContext());
Notification notification = notificationBuilder.build();
notificationManager.notify(0, notification);
}
the MessageNotificationActivity is using the "android:style/Theme.Dialog" to make it look like a dialog now the thing is that when I click on the notification everything goes well and the activity is opened like a dialog with nothing in the background but if the app is paused and is in background when I click the notification it brings the apps Main activity to the front and then opens the MessageNotiicationActivity on top of it. How can I make the notification not bring the main activity to front and only intent to the specefied activity?
The reason this happens is that your dialog-themed Activity has the same "task affinity" as the other activities in your app, so when you click on the notification, Android looks for an appropriate task in which to launch the dialog-themed Activity. If your app is in the background, Android will see it as a task with the same "task affinity" as the dialog-themed Activity and bring that task to the foreground and launch the dialog-themed Activity into that task.
To prevent this from happening, you need to add android:taskAffinity="" to the <activity> declaration in the manifest for your dialog-themed Activity. This tells Android to launch the dialog-themed Activity into a new task, and not into your app's task.
Try adding the flag Intent.FLAG_ACTIVITY_CLEAR_TASK to your intent. This will clear any other activities in the task.

Using PendingIntent OnFinished callback from a Notification Action

Actions in Android Notifications require a PendingIntent to operate. Typical examples show an action triggering an app Activity. I'm trying to use a Notification action to open an Android Intent - Rate the app in the play store or send an email.
Intent intentRate = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + mThis.getPackageName()))
Intent intentEmail = new Intent(Intent.ACTION_SEND ...
I would like to make use of the OnFinished PendingIntent callback, but those are called through the .send() method and the syntax for using PendingIntents in Actions don't include .send()
Notification.Builder myRatingNotification = new Notification.Builder(mThis)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.mipmap.ic_launcher)
.addAction(0, mThis.getString(R.string.Rate_Act_Now), PendingIntent.getActivity(mThis, 0, intentRate, PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(0, mThis.getString(R.string.Rate_App_Email), PendingIntent.getActivity(mThis, 0, intentEmail, PendingIntent.FLAG_UPDATE_CURRENT));
Notification notification = new Notification.BigTextStyle(myRatingNotification).bigText(text).build();
((NotificationManager) mThis.getSystemService(Context.NOTIFICATION_SERVICE)).notify(notificationId, notification);
This is all running in a BroadcastReceiver service in the background, by the way. The user would typically never open the app directly except for first-use setup. So this interaction can only happen via Notification.
How can I get my app to run a little bit of code after the action is chosen and completed?

What is the difference between Notification and NotificationManger in Android?

What is the difference between those two?
I want to use the startForeground method and cannot use it with NotificationManager..
Thanks
A Notification is a class that represents either a persistent icon that goes in the status bar and is accessible through the launcher, turning on or flashing LEDs on the device, or alerting the user by flashing the backlight, playing a sound, or vibrating.
The Notification Manager is the class that allows you to add notifications to the system,
startForeground is a method of the Serivce class. For example inside your Service class you can have something like this.
Intent notificationIntent = new Intent(this, ActivityMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_play)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setTicker(getString(R.string.app_name))
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setContentTitle(getString(R.string.app_name))
.setContentText(someText);
Notification notification = builder.build();
startForeground(1, notification);
A Notification is a description of what you want to occur to alert the user about something -- what icon goes in the status bar, what ringtone to play, etc.
NotificationManager is a system service that can show a Notification.
I want to use the startForeground methode and cannot use it with NotificationManager
Correct. Create a Notification using Notification.Builder (or NotificationCompat.Builder). See this project for an example of using startForeground().

Categories

Resources