Cordova - Notify background running service - android

I am using cordova to build my android application. Since android kills service, i am binding service with a notification to avoid service kill.
Here is my method how i bind the service with notification
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
context = this.getApplicationContext();
notifyService();
return START_NOT_STICKY;
}
private void notifyService() {
String package_name = this.getApplication().getPackageName();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
this.getApplication().getResources().getIdentifier("icon", "drawable-hdpi", package_name));
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Smart Home")
.setContentText("Smart Home running in background")
.setSmallIcon(this.getApplication().getResources().getIdentifier("icon", "drawable-hdpi", package_name))
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
startForeground(notificationId, notification);
}
Here's the output
Notification is generated but notification title is not as i set. Also, when i click this notification, it's moving to app info activity. But i want to move to my main activity.
Does anyone faced this same issue? Or my code need any change for cordova?

when i click this notification, it's moving to app info activity. But i want to move to my main activity to achieve this
change this line
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
to this line
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Hope it helps you

Figured it out after a long try. Problem was with my pending intent activity name. Below code worked for me
String package_name = this.getApplication().getPackageName();
Intent notificationIntent = context.getPackageManager().getLaunchIntentForPackage(package_name);

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;
}

Foreground notification service sometimes gets stopped on its own and after sometime starts automatically in nougat

I have a never-ending foreground service which shows a notification, but in my device which is having an Android version as nougat.
For some time the notification is not visible(i think most probably because the service has stopped working) and after some time it is visible again automatically.
So I am not able to detect what exactly can be a reason for this.
Attaching the code part in which I have started the service.
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//Log.i(LOG_TAG, "Received Start Foreground Intent ");
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
RemoteViews notificationView = new RemoteViews(this.getPackageName(), R.layout.notification);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Notification notification = new NotificationCompat.Builder(this,"100")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setColor(Color.parseColor("#00f6d8"))
.setContent(notificationView)
.setPriority(Notification.PRIORITY_MIN)
.setOngoing(true).build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
return START_REDELIVER_INTENT;
}

How can I put notification icon in notification area while app is running android?

I want to place my app icon in notification area while my app is running. I searched a lot and made this possible, to show it in the notification area.
public class MainActivity extends ActionBarActivity {
NotificationManager mNotificationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getApplicationContext();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setTicker("This is a new notification")
.setContentTitle("Notification")
.setContentText("App runing..")
.setSmallIcon(R.mipmap.ic_launcher);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(pIntent);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = builder.build();
mNotificationManager.notify(1, notif);
}
#Override
protected void onDestroy() {
super.onDestroy();
mNotificationManager.cancel(1);
}
}
But what I want is I don't want to open any activity when clicking the notification.
eg: the battery info icon comming when battery is full, it willnot open any application or activity, also tapping it willnot remove it from notification area.
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
By changing this one line code to this
PendingIntent pIntent = PendingIntent.getActivity(context, 0, null, 0);
It will work like what I want, But only on emulator, when I run this code in real device it will crash.
how to achieve this task in my app? How can I put notification icon without Intent and PendingIntent ?
You could use this code, like mentioned in the answers here and here:
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
new Intent(), // add this
PendingIntent.FLAG_UPDATE_CURRENT);
It is not about the notification or notification manager
you can do it by changing your one line code to this,
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);

Notification does not work properly on some devices

In my app I use a service for background work(send data to server) when my app is in the background.So I create a notification which tells to the user that app is running in the background.I want when the user taps on the notification my app to come to the foreground so I use an Intent like this that android use to launch my app
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.final_driver_notification_icon)
.setContentTitle("...")
.setContentText("...");
final Intent notintent = new Intent(getApplicationContext(),EntryScreen.class);
notintent.setAction(Intent.ACTION_MAIN);
notintent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendint = PendingIntent.getActivity(this, 0, notintent, 0);
mBuilder.setContentIntent(pendint);
int notID = 001;
startForeground(notID, mBuilder.build());
In the emulator and some physical devices works perfect.But in some other it launches the app from the start(It puts Entry screen in the top).
Any idea why is this happening?
You should start your service with startForeground(). this is the way to start a service in foreground and report by a notification. Android - implementing startForeground for a service?
or try something like this:
public void initForeground() {
Intent intentForeground = new Intent(this, Activity.class);
intentForeground.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentForeground,0);
pendingIntent.cancel();
pendingIntent = PendingIntent.getActivity(this, 0, intentForeground,0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.icon)
.setContentIntent(pendingIntent)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.something))
.setTicker(getString(R.string.something))
.setAutoCancel(false);
Notification notification = mBuilder.build();
startForeground(myID, notification);
}
Hope it helps you!

Android Notification Action not calling pending intent to service

I am trying to get my on going notification to have a button, when clicked it should call my service which is controlling audio playback.
Here is my notification with the intent
Intent intent = new Intent(context, AudioStreamService.class);
Random generator = new Random();
PendingIntent i = PendingIntent.getActivity(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(text)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setLargeIcon(picture)
.setTicker(ticker)
.setNumber(number)
.setOngoing(true)
.addAction(
R.drawable.ic_action_stat_reply,
res.getString(R.string.action_reply),
i);
notify(context, builder.build());
Here is the on start for my service
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("APP ID", "APP ID - service called ");
if(isPlaying){
stop();
}
else {
play();
}
return Service.START_STICKY;
}
The log is never triggered when called from the action in the notification. But the service is used by a button in the app and works fine. Log gets called as do commands below.
Use :
PendingIntent pendingIntent = PendingIntent.getService(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT);
instead of :
PendingIntent pendingIntent = PendingIntent.getActivity(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT);
^^ This is used to start an activity from the notification.

Categories

Resources