android: how to handle taps on foreground notifications - android

I have a foreground Service that displays a persistent notification like so:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
createNotificationChannel(); // Creating channel for API 26+
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("This is my service")
.setContentText("Tap to open configuration")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(contentIntent)
.setOngoing(true)
.build();
startForeground(SERVICE_NOTIFICATION_ID, notification);
return Service.START_STICKY;
}
Currently, tapping on this notification opens the app just like if I press the app icon. No extra info is passed. I need to tell if the app was opened from the notification and not from the app icon.
Now, The docs say I can start some other activity, but my app is a React Native app, and I only have one real activity: MainActivity. The rest is in JS.
I'm assuming I need to create a BroadcastReceiver and somehow listen to intents that get sent while the user pushes the button. Or maybe I can handle intents in my MainActivity or some Service and emit events to JS from there. I am a bit lost. What's the best way to go about this?

You can try to add an extra flag to the notificationIntent like this:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("FROM_NOTIFICATION", true);
Then you can retrieve that flag in MainActivity.onCreate() like this:
isFromNotification = getIntent().getBooleanExtra("FROM_NOTIFICATION", false);
Also you may need to change the flag PendingIntent.FLAG_CANCEL_CURRENT to PendingIntent.FLAG_UPDATE_CURRENT.

Related

Trying to run application in the background (Minimized/Screen Locked/Removed from running application)

I am writing an android application which will trigger the SMS once a missed call is detected. So far I am able to detect missed call and send the SMS to the caller.
If I run my app, (Turned on the SWITCH (like a smart switch) that checks for the ringing call and detects missed call), and then someone is calling and if it is a missed call, the SMS is fired correctly.
If I minimize my app (Do not lock phone) and open Instagram/Other app, then also SMS is going to caller.
BUT,
when my app (Smart switch to enable this feature is ON), but I close my app, no SMS sent.
when my app is minimized (Screen Locked), no message fired. (Smart switch ON)
when my app is opened (Screen locked), no message fired. (Smart switch ON)
I am new to android. Please help me, I want to keep my app running if my SMART SWITCH is on, the my app will continue to monitor missed call and fire SMS to caller irrespective of APP in background or not, screen locked or not as long as my Smart switch is ON.
I tried to start a service and it worked, for background to work, we need a foreground.
public class myservices extends Service
{
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Intent notificationIntent = new Intent(this, MainActivity.class); //To open the MainActivity onClick
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Intent busy = new Intent(this, InterceptCall.class);
Intent driving = new Intent(this, InterceptCall.class);
Intent meeting = new Intent(this, InterceptCall.class);
busy.putExtra("mode","busy");
driving.putExtra("mode", "driving");
meeting.putExtra("mode", "meeting");
PendingIntent busyIntent = PendingIntent.getBroadcast(this, 1, busy, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
PendingIntent drivingIntent = PendingIntent.getBroadcast(this, 2, driving, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
PendingIntent meetingIntent = PendingIntent.getBroadcast(this, 3, meeting, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Smart Mode is on ")
.setContentText("Drag down to display modes.")
.setContentIntent(pendingIntent)
.setColor(Color.MAGENTA)
.setSmallIcon(R.drawable.pingedlogo)
.addAction(R.mipmap.ic_launcher, "Busy", busyIntent)
.addAction(R.mipmap.ic_launcher, "Driving", drivingIntent)
.addAction(R.mipmap.ic_launcher, "Meeting", meetingIntent)
.build();
startForeground(1, notification);
return START_NOT_STICKY;
}
The first you need to have the permission about task
<uses-permission android:name="android.permission.WAKE_LOCK" />
Code example
Thread(Runnable {
// a potentially time consuming task
val bitmap = processBitMap("image.png")
imageView.post {
imageView.setImageBitmap(bitmap)
}
}).start()
Also you need to
I recommend to look up the next page
https://developer.android.com/guide/components/processes-and-threads
Services overview
This is how I solved it.
public class myservices extends Service
{
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Intent notificationIntent = new Intent(this, MainActivity.class); //To open the MainActivity onClick
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Intent busy = new Intent(this, InterceptCall.class);
Intent driving = new Intent(this, InterceptCall.class);
Intent meeting = new Intent(this, InterceptCall.class);
busy.putExtra("mode","busy");
driving.putExtra("mode", "driving");
meeting.putExtra("mode", "meeting");
PendingIntent busyIntent = PendingIntent.getBroadcast(this, 1, busy, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
PendingIntent drivingIntent = PendingIntent.getBroadcast(this, 2, driving, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
PendingIntent meetingIntent = PendingIntent.getBroadcast(this, 3, meeting, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Smart Mode is on ")
.setContentText("Drag down to display modes.")
.setContentIntent(pendingIntent)
.setColor(Color.MAGENTA)
.setSmallIcon(R.drawable.pingedlogo)
.addAction(R.mipmap.ic_launcher, "Busy", busyIntent)
.addAction(R.mipmap.ic_launcher, "Driving", drivingIntent)
.addAction(R.mipmap.ic_launcher, "Meeting", meetingIntent)
.build();
startForeground(1, notification);
return START_NOT_STICKY;
}

Cannot set anything to stickynotification

I am starting stickynotification from my service using startForeground. Notification does show up however my settings, like title or intent to show up when clicked does not take effect.
#Override
public void onCreate()
{
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Notification notification = new NotificationCompat.Builder(this)
//.setSmallIcon(R.mipmap.app_icon)
.setContentTitle("My Awesome App")
.setContentText("Doing some work...")
.setContentInfo("Server is running")
.setContentIntent(pendingIntent).build();
startForeground(1337, notification);
super.onCreate();
}
Notification shows app name and touch for more info or to stop....
When clicked it takes me to app info where i can forestop or uninstall it.
If anybody out there in this universe having same problem i suggest do not overclock your brain else you will endup missing small things like requesting permissions.
Setting up icon fixed my issue.

How to perform notification-action (click) on lock-screen?

TL;DR
How can I make a notification that does some work from the lock-screen without unlocking? After clicking an action, a button on the notification or just the complete notification, I want to do an API call (without typing my unlock code)
Details
Goal
Based on the answer on this question I tried to make a notification with an action that works on the lockscreen without unlocking the device. The action is something that doesn't need any further interface or interaction (think 'send an API request').
Status
The notification and click do work with an unlocked device. However, when locked I still need to enter the unlock code first, so either there is something new going on, or I just misunderstood the way it is supposed to work.
If I understand correctly I can set my visibility to 'public' to show the content (this works), and instead of defining an action (which does't seem to be public) I can handle clicks on the (now visible) layout. I tried this with the below code, but obviously it doesn't work.
I have tried both sending the intent to my app and to a service, as florian suggested below.
Code
This is code where I start the notification (this lives in an Activity, code was shortened for your convenience )
private void startNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
.setContentTitle("title text")
.setContentText("content text");
Intent openIntent = new Intent(MyMainActivity.this, MyMainActivity.class);
openIntent.setAction("some_string");
PendingIntent pOpenIntent = PendingIntent.getActivity(this, 0, openIntent, 0);
builder.setContentIntent(pOpenIntent);
RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
builder.setContent(view);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
}
As said, I also tried with the service as florian suggested, with this as a call:
Intent yepIntent = new Intent(this, MyIntentService.class);
yepIntent.setAction("test");
yepIntent.putExtra("foo", true);
yepIntent.putExtra("bar", "more info");
PendingIntent yepPendingIntent = PendingIntent.getService(this, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//builder.addAction(R.drawable.abc_ic_menu_share_mtrl_alpha, "My Action", yepPendingIntent);
builder.setContentIntent(yepPendingIntent);
The action didn't show up on the lock-screen, so I changed it to the setContentIntent you see above. The result is the same though, no action for me :(
Try using an IntentService.
Replace your intent target with your intent service:
Intent yepIntent = new Intent(context, MyIntentService.class);
yepIntent.putExtra("foo", true);
yepIntent.putExtra("bar", "more info");
PendingIntent yepPendingIntent = PendingIntent.getService(context, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.addAction(R.drawable.icon_of_choice, "My Action", yepPendingIntent);
Register your service in the Manifest:
<service
android:name="app.great.mypackage.MyIntentService"
android:exported="false"/>
Your Service could look like this:
public class MyIntentSerice extends IntentService {
#Override
protected void onHandleIntent(Intent intent) {
Log.d("myapp", "I got this awesome intent and will now do stuff in the background!");
// .... do what you like
}
}
UPDATE with feedback from Nanne
The trick seems to be to
Use a service
Add the intent not as an action or a contentIntent, but with the RemoteViews method.
Combined it will be:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
.setContentTitle("My notification")
.setContentText("Hello World!");
int notificationId = 1;
Intent yepIntent = new Intent(this, MyIntentService.class);
yepIntent.setAction("test");
yepIntent.putExtra("foo", true);
yepIntent.putExtra("bar", "more info");
PendingIntent yepPendingIntent = PendingIntent.getService(this, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// doesn't show up on my lock-screen
//builder.addAction(R.drawable.abc_ic_menu_share_mtrl_alpha, "My Action", yepPendingIntent);
// asks for unlock code for some reason
//builder.setContentIntent(yepPendingIntent);
// Bingo
RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
view.setOnClickPendingIntent(R.id.notification_closebtn_ib, yepPendingIntent);
builder.setContent(view);
Combining the answer from the question I linked (Notification action button not clickable in lock screen) and the one #florian_barth gave above, I got it working
The trick seems to be to
Use a service
Add the intent not as an action or a contentIntent, but with the RemoteViews method.
Combined it will be:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
.setContentTitle("My notification")
.setContentText("Hello World!");
int notificationId = 1;
Intent yepIntent = new Intent(this, MyIntentService.class);
yepIntent.setAction("test");
yepIntent.putExtra("foo", true);
yepIntent.putExtra("bar", "more info");
PendingIntent yepPendingIntent = PendingIntent.getService(this, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// doesn't show up on my lock-screen
//builder.addAction(R.drawable.abc_ic_menu_share_mtrl_alpha, "My Action", yepPendingIntent);
// asks for unlock code for some reason
//builder.setContentIntent(yepPendingIntent);
// Bingo
RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
view.setOnClickPendingIntent(R.id.notification_closebtn_ib, yepPendingIntent);
builder.setContent(view);
It also works with Broadcast receiver and setAction
PendingIntent pendingIntent = PendingIntent.getBroadcast(..
builder.addAction(..
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
On the lock screen swipe down on the notification to expand it and tap the action area to invoke the broadcast receiver without unlocking the phone.

In Android How to Restart App On Click Application "onGoing" Notification. if App is open or not

In Android How to Restart App On Click Application "onGoing" Notification. if App is open or not.
Like As When i Click on onGoing Notification "Connected as a Media device"
You can define actions you want to happen when interacting with your Notification by adding a PendingIntent.
In the following example a PendingIntent is created to launch the (current) activity.
That intent is then added to the notification in the content section. Once this notification is shown, when you click the content section, the intent is fired and the app gets started or brought back to top.
private static final int NOTIFICATION_ID = 1;
...
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
nb.setSmallIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(getText(R.string.app_name))
.setContentText("Click to launch!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent) // Here the "onClick" Intent is added
.setOngoing(false);
nm.notify(NOTIFICATION_ID, nb.build());
In this case the notification is dismissable. If you set .setOngoing(true) you need to remove it by calling .cancel(NOTIFICATION_ID) on an instance of the NotificationManager.
See also this introduction on how to Build a Notification.

Transaction to a specific fragment if we receive a push notification while app is running

Is it possible not launch new activity if we receive a push notification while the app is running?
My activity works with fragments and I want to do transition to a determinate fragment when the notification is received. My activity have data that I need to show the fragments. The problem is that when I receive the push notification while the app is running the method onDestroy is called and here I clear the data and then the app crash because the data are null. How can I do to not create new activity when the app receive a push notification while is running? In case the app is running I want that if you click the notification do a transition fragment, not create again the activity.
Thanks in advance.
First of all, I think that you mean "notification" to be a "message", but not android.app.Notification class.
And second, I don't think it's a best practise to raise new GUI when receiving a message, which would interrupt the user interaction. For details, please refer to: http://developer.android.com/training/notify-user/index.html and http://developer.android.com/design/patterns/notifications.html.
At last, if you really wanna do what you stated in your thread, I wonder why the data used to generate the show-data fragment is held in the activity. Try holding the data in an android.app.IntentService object, and then generate transfer the data to new activity, and then use android.app.Fragment.setArguments method to transfer the data from activity to fragment.
I think that this code will help you. This which you need is PendingIntent, it make transaction to desired activity.
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String title,
String message) {
//get the default notification sound URI
Uri uriNotificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//make intent to the application. if the application is opened just maximize it.
Intent homeIntent = new Intent(context, 'your desired activity');
homeIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
homeIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("eCommCongress")
.setContentText(message)
.setLights(Color.GREEN, 1500, 1500)
.setSound(uriNotificationSound)
.setAutoCancel(true)
.setContentIntent(contentIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(counter, mBuilder.build());
counter++;
}
It is perfectly possible and I do such a thing in one of my apps. First, you need to declare your activity as android:launchMode="singleTop",
Then, when you build you must configure your pending intent not to fire a new instance of your activity:
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent("YOUR ACTION HERE");
intent.setClassName(this, MainActivity.class.getName());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(idNotificacion,notification);
Now all you have to do is to override your onNewIntent inside your Activity and do whatever you want with your fragment:
#Override
protected void onNewIntent(Intent intent) {
MiLog.i(getApplicationContext(),"IntentShit","new intent received");
MiLog.i(getApplicationContext(),"IntentShit","Action: "+intent.getAction());
if(intent.getAction()!=null && intent.getAction().equals("YOUR ACTION HERE"){
//DO your stuff here
}
}
You should also take a look at this page for more info:
http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode

Categories

Resources